Python Lists offers a wide range of built-in methods to manipulate data efficiently. List in Python, is an ordered and mutable data type allowing us to store multiple values in a single variable.
Let us take a look at various List methods available in Python.
| Method | Description |
|---|---|
| append() | This method is utilized to add an element x to the end of the list. |
| extend() | This method is utilized to add all elements of an iterable (list, tuple, etc.) to the list. |
| insert() | This method is utilized to insert an element x at index i. |
| remove() | This method is utilized to remove the first occurrence of x. It raises ValueError if x is not found. |
| pop() | This method is utilized to remove and returns the element at index i (default is the last element). |
| clear() | This method is utilized to remove all elements, making the list empty. |
| index() | This method is utilized to return the first index of x between start and end. It raises ValueError if not found. |
| count() | This method is utilized to return the number of occurrences of x in the list. |
| sort() | This method is utilized to sort the list in place (default is ascending). |
| reverse() | This method is utilized to reverse the order of the list in place. |
| copy() | This method is utilized to return a shallow copy of the list. |
The append() method in Python allows us to add an element at the end of the list.
It has the following syntax:
Let us take an example to demonstrate the append() method in Python.
Output:
List of Fruits: ['apple', 'mango', 'banana', 'orange', 'guava'] Updated List of Fruits: ['apple', 'mango', 'banana', 'orange', 'guava', 'grapes']
Explanation:
In the above example, we have used the append() method to add a new element 'grapes' at the end of the given list.
The extend() method in Python allows us to append all the elements from an iterable (list, tuple, set, etc.) at the end of the list.
It has the following syntax:
Let us take an example to demonstrate the extend() method in Python.
Output:
Old Shopping List: ['milk', 'bread', 'butter'] New Items: ['eggs', 'apples', 'coffee'] Updated Shopping List: ['milk', 'bread', 'butter', 'eggs', 'apples', 'coffee']
Explanation:
In the above example, we have used the extend() method to add an iterable at the end of the given list.
The insert() method in Python allows us to insert an the elements at a specified position in the list.
It has the following syntax:
Let us take an example to illustrate how the insert() method works in Python.
Output:
Old Shopping List: ['milk', 'bread', 'butter', 'coffee'] New Shopping List: ['milk', 'bread', 'eggs', 'butter', 'coffee']
Explanation:
In the above example, we have used the insert() method to insert item 'eggs' at the second index of the given list.
The remove() method in Python allows us to remove the first occurrence of the specified element from the list.
It has the following syntax:
Let us take an example to demonstrate the remove() method in Python.
Output:
Old Shopping List: ['milk', 'bread', 'eggs', 'butter', 'coffee'] New Shopping List: ['milk', 'bread', 'eggs', 'coffee']
Explanation:
In the above example, we have used the remove() method to remove item 'butter' from the given list.
The pop() method in Python allows us to remove and return the element from the specified index of the given list.
It has the following syntax:
Let us take an example to demonstrate the pop() method in Python.
Output:
Old Shopping List: ['milk', 'bread', 'eggs', 'butter', 'coffee'] New Shopping List: ['milk', 'eggs', 'butter', 'coffee'] Popped Item: bread
Explanation:
In the above example, we have used the pop() method to remove and return the item at index '2' from the given list.
The clear() method in Python allows us to remove all the elements from the list making it empty.
It has the following syntax:
Let us take an example to demonstrate the clear() method in Python.
Output:
Old Shopping List: ['milk', 'bread', 'eggs', 'butter', 'coffee'] New Shopping List: []
Explanation:
In the above example, we have used the clear() method to remove all the elements from the given list.
The index() method in Python returns the first index of the element within start and end.
It has the following syntax:
Let us take an example to demonstrate the index() method in Python.
Output:
Shopping List: ['milk', 'bread', 'eggs', 'butter', 'coffee'] Index of butter: 3
Explanation:
In the above example, we have used the index() method to find the first occurrence of the element from the given list.
The count() method in Python allows us to all the occurrence of the element in the list.
It has the following syntax:
Let us take an example to demonstrate the count() method in Python.
Output:
Shopping List: ['milk', 'bread', 'egg', 'milk', 'butter', 'coffee', 'egg', 'flour', 'egg'] No. of Eggs: 3
Explanation:
In the above example, we have used the count() method to find all the occurrences of 'egg' in the given list.
The sort() method in Python allows us to sort the list in place.
It has the following syntax:
Let us take an example to illustrate how the sort() method works in Python.
Output:
Unsorted Fruit Basket: ['mango', 'apple', 'orange', 'banana', 'grapes'] Sorted Fruit Basket: ['apple', 'banana', 'grapes', 'mango', 'orange']
Explanation:
In the above example, we have used the sort() method to sort the elements of the given list in ascending order.
The reverse() method in Python allows us to reverse the list in place.
It has the following syntax:
Let us take an example to illustrate how the reverse() method works in Python.
Output:
Fruit Basket: ['apple', 'banana', 'grapes', 'mango', 'orange'] Reversed Fruit Basket: ['orange', 'mango', 'grapes', 'banana', 'apple']
Explanation:
In the above example, we have used the reverse() method to reverse the given list.
The copy() method in Python allows us to create a shallow copy of the list.
It has the following syntax:
Let us take an example to illustrate how the copy() method works in Python.
Output:
Fruit Basket: ['apple', 'banana', 'grapes', 'mango', 'orange'] Copied Fruit Basket: ['apple', 'banana', 'grapes', 'mango', 'orange']
Explanation:
In the above example, we have used the copy() method to create a shallow copy of the given list.
We request you to subscribe our newsletter for upcoming updates.