Python Lists provide a variety of built-in functions that facilitate efficient data manipulation. A List in Python is an ordered and mutable data structure that enables the storage of numerous values within a single variable.
Let's explore the different List methods that Python offers.
| 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. |
1) append
In Python, the append function enables the addition of an element to the conclusion of a list.
Syntax:
It has the following syntax:
list_name.append(item)
Python append Method Example
Consider the following example to illustrate the use of the append method in Python.
Example
# python program to show the use of list append() method
# creating a list
list_of_fruits = ['apple', 'mango', 'banana', 'orange', 'guava']
# printing the list
print("List of Fruits:", list_of_fruits)
# using the append() method
list_of_fruits.append('grapes')
# printing the updated list
print("Updated List of Fruits:", list_of_fruits)
Output:
List of Fruits: ['apple', 'mango', 'banana', 'orange', 'guava']
Updated List of Fruits: ['apple', 'mango', 'banana', 'orange', 'guava', 'grapes']
Explanation:
In the preceding illustration, the append function has been utilized to insert a new item, 'grapes', at the conclusion of the specified list.
2) extend
In Python, the extend function provides the capability to add all items from an iterable (such as a list, tuple, set, etc.) to the conclusion of an existing list.
Syntax:
It has the following syntax:
list_name.extend(iterable)
Python extend Method Example
Let us consider an example to illustrate the use of the extend method in Python.
Example
# python program to show the use of list extend() method
# initializing the lists
shopping_list = ['milk', 'bread', 'butter']
new_items = ['eggs', 'apples', 'coffee']
# printing the lists
print("Old Shopping List:", shopping_list)
print("New Items:", new_items)
# using the extend() method
shopping_list.extend(new_items)
# printing the updated list
print("Updated Shopping List:", shopping_list)
Output:
Old Shopping List: ['milk', 'bread', 'butter']
New Items: ['eggs', 'apples', 'coffee']
Updated Shopping List: ['milk', 'bread', 'butter', 'eggs', 'apples', 'coffee']
Explanation:
In the example provided, we utilized the extend function to append an iterable to the conclusion of the specified list.
3) insert
In Python, the insert method enables us to place elements at a designated index within a list.
Syntax:
It has the following syntax:
list_name.insert(index, item)
Python insert Method Example
Let’s consider an example to demonstrate the functionality of the insert method in Python.
Example
# python program to show the use of list insert() method
# initializing the list
shopping_list = ['milk', 'bread', 'butter', 'coffee']
# printing the list
print("Old Shopping List:", shopping_list)
# using the insert() method
shopping_list.insert(2, 'eggs')
# printing the updated list
print("New Shopping List:", shopping_list)
Output:
Old Shopping List: ['milk', 'bread', 'butter', 'coffee']
New Shopping List: ['milk', 'bread', 'eggs', 'butter', 'coffee']
Explanation:
In the preceding example, the insert function has been utilized to place the item 'eggs' at the second index of the specified list.
4) remove
In Python, the remove function enables us to eliminate the first instance of a designated item from a list.
Syntax:
It has the following syntax:
list_name.remove(item)
Python remove Method Example
Let’s consider an example to illustrate the use of the remove method in Python.
Example
# python program to show the use of list remove() method
# initializing the list
shopping_list = ['milk', 'bread', 'eggs', 'butter', 'coffee']
# printing the list
print("Old Shopping List:", shopping_list)
# using the remove() method
shopping_list.remove('butter')
# printing the updated list
print("New Shopping List:", shopping_list)
Output:
Old Shopping List: ['milk', 'bread', 'eggs', 'butter', 'coffee']
New Shopping List: ['milk', 'bread', 'eggs', 'coffee']
Explanation:
In the preceding example, the remove function has been utilized to eliminate the item 'butter' from the specified list.
5) pop
In Python, the pop function enables us to eliminate and retrieve the element located at a specific index within a provided list.
Syntax:
It has the following syntax:
list_name.pop(index)
Python pop Method Example
To illustrate the functionality of the pop method in Python, let us consider an example.
Example
# python program to show the use of list pop() method
# initializing the list
shopping_list = ['milk', 'bread', 'eggs', 'butter', 'coffee']
# printing the list
print("Old Shopping List:", shopping_list)
# using the pop() method
popped_item = shopping_list.pop(1)
# printing the updated list
print("New Shopping List:", shopping_list)
print("Popped Item:", popped_item) # popped element
Output:
Old Shopping List: ['milk', 'bread', 'eggs', 'butter', 'coffee']
New Shopping List: ['milk', 'eggs', 'butter', 'coffee']
Popped Item: bread
Explanation:
In the preceding illustration, the pop function has been utilized to eliminate and retrieve the element located at index '2' from the specified list.
6) clear
In Python, the clear function enables users to eliminate all items from a list, resulting in an empty list.
Syntax:
It has the following syntax:
list_name.clear()
Python clear Method Example
To illustrate the functionality of the clear method in Python, let us consider an example.
Example
# python program to show the use of list clear() method
# initializing the list
shopping_list = ['milk', 'bread', 'eggs', 'butter', 'coffee']
# printing the list
print("Old Shopping List:", shopping_list)
# using the clear() method
shopping_list.clear()
# printing the updated list
print("New Shopping List:", shopping_list)
Output:
Old Shopping List: ['milk', 'bread', 'eggs', 'butter', 'coffee']
New Shopping List: []
Explanation:
In the preceding example, the clear method has been utilized to eliminate all the items from the specified list.
7) index
In Python, the index function retrieves the initial index of a specified element within the defined start and end parameters.
Syntax:
It has the following syntax:
list_name.index(item, start, end)
Python index Method Example
To illustrate the functionality of the index method in Python, let’s consider a practical example.
Example
# python program to show the use of list index() method
# initializing the list
shopping_list = ['milk', 'bread', 'eggs', 'butter', 'coffee']
# printing the lists
print("Shopping List:", shopping_list)
# using the index() method
item_index = shopping_list.index('butter')
# printing the index of the item
print("Index of butter:", item_index)
Output:
Shopping List: ['milk', 'bread', 'eggs', 'butter', 'coffee']
Index of butter: 3
Explanation:
In the preceding illustration, we utilized the index function to locate the initial appearance of the specified element within the provided list.
8) count
In Python, the count function enables us to determine the total number of times a specific element appears within a list.
Syntax:
It has the following syntax:
list_name.count(item)
Python count Method Example
To illustrate the functionality of the count method in Python, let us consider an example.
Example
# python program to show the use of list count() method
# initializing the list
shopping_list = ['milk', 'bread', 'egg', 'milk', 'butter', 'coffee', 'egg', 'flour', 'egg']
# printing the lists
print("Shopping List:", shopping_list)
# using the count() method
item_count = shopping_list.count('egg')
# printing the count of the item
print("No. of Eggs:", item_count)
Output:
Shopping List: ['milk', 'bread', 'egg', 'milk', 'butter', 'coffee', 'egg', 'flour', 'egg']
No. of Eggs: 3
Explanation:
In the preceding illustration, the count function has been employed to determine the total instances of 'egg' within the specified list.
9) sort
In Python, the sort function provides the capability to arrange the elements of a list directly within the original list itself.
Syntax:
It has the following syntax:
list_name.sort(reverse = False, key = None)
Python sort Method Example
To demonstrate the functionality of the sort method in Python, let us consider an example.
Example
# python program to show the use of list sort() method
# initializing the list
fruit_basket = ['mango', 'apple', 'orange', 'banana', 'grapes']
# printing the list
print("Unsorted Fruit Basket:", fruit_basket)
# using the sort() method
fruit_basket.sort()
# printing the updated list
print("Sorted Fruit Basket:", fruit_basket)
Output:
Unsorted Fruit Basket: ['mango', 'apple', 'orange', 'banana', 'grapes']
Sorted Fruit Basket: ['apple', 'banana', 'grapes', 'mango', 'orange']
Explanation:
In the preceding illustration, the sort function has been utilized to arrange the items within the specified list in ascending sequence.
10) reverse
In Python, the reverse method enables the reversal of a list directly within its original location.
Syntax:
It has the following syntax:
list_name.reverse()
Python reverse Method Example
To demonstrate the functionality of the reverse method in Python, let us consider an example.
Example
# python program to show the use of list reverse() method
# initializing the list
fruit_basket = ['apple', 'banana', 'grapes', 'mango', 'orange']
# printing the list
print("Fruit Basket:", fruit_basket)
# using the reverse() method
fruit_basket.reverse()
# printing the updated list
print("Reversed Fruit Basket:", fruit_basket)
Output:
Fruit Basket: ['apple', 'banana', 'grapes', 'mango', 'orange']
Reversed Fruit Basket: ['orange', 'mango', 'grapes', 'banana', 'apple']
Explanation:
In the example provided above, the reverse function has been utilized to invert the specified list.
11) copy
In Python, the copy method enables the creation of a shallow duplicate of a list.
Syntax:
It has the following syntax:
list_name.copy()
Python copy Method Example
To demonstrate the functionality of the copy method in Python, let's consider an example.
Example
# python program to show the use of list copy() method
# initializing the list
fruit_basket = ['apple', 'banana', 'grapes', 'mango', 'orange']
# printing the list
print("Fruit Basket:", fruit_basket)
# using the copy() method
new_fruit_basket = fruit_basket.copy()
# printing the copied list
print("Copied Fruit Basket:", new_fruit_basket)
Output:
Fruit Basket: ['apple', 'banana', 'grapes', 'mango', 'orange']
Copied Fruit Basket: ['apple', 'banana', 'grapes', 'mango', 'orange']
Explanation:
In the aforementioned example, the copy function has been utilized to generate a shallow duplicate of the specified list.
Python List Methods – FAQs
1. What is the difference between append and extend?
The append function is utilized to insert a solitary item at the conclusion of the list, while the extend function is employed to incorporate several items into the list.
Let’s examine an illustration that highlights the primary distinction between the two.
Example
# creating a list
num_lst = [2, 5, 9]
# printing the results
num_lst.append([3, 6]) # appending
print(num_lst)
num_lst.extend([1, 4]) # extending
print(num_lst)
Output:
[2, 5, 9, [3, 6]]
[2, 5, 9, [3, 6], 1, 4]
Explanation:
In the example provided, it is evident that the append function has inserted [3, 6] into the list as a singular entity, whereas the extend function has incorporated [1, 4] into the list as distinct elements.
2. How do we remove an element from a list?
To eliminate an element from a list, Python provides several list manipulation methods. Among these methods are:
- remove: This function removes the first instance of the specified element from the list.
- pop: This function takes an index and removes the corresponding element from the list.
Below is an illustration demonstrating the functionality of the remove and pop methods.
Example
# creating a list
num_lst = [2, 5, 9, 20, 15, 9]
# printing the results
num_lst.remove(9) # removing first occurrence of 9 from the list
print(num_lst)
num_lst.pop(-2) # popping the element of the index: -2
print(num_lst)
Output:
[2, 5, 20, 15, 9]
[2, 5, 20, 9]
Explanation:
In the example provided, the remove function has been utilized to eliminate the initial instance of 9 from the list. Meanwhile, the pop function has extracted the element 15 from the list, which is located at the index -2.
3. How do we find the index of an element in a list?
The index of the initial appearance of a specific element within a list can be retrieved by utilizing the list's index function. Below is a straightforward illustration demonstrating how to use the index method.
Example
# creating a list
num_lst = [2, 5, 9, 20, 15]
# printing the index of 9
print(num_lst.index(9))
Output:
Explanation:
In the preceding illustration, the index function has been utilized to determine the position of the element 9 within the list.
4. How do we sort a list in Python?
Python provides a built-in method known as sort that allows for in-place sorting of a list. This functionality can be illustrated through the following example:
Example
# creating a list
num_lst = [2, 9, 5, 20, 15]
# sorting the list
num_lst.sort()
# printing the sorted list
print(num_lst)
Output:
[2, 5, 9, 15, 20]
Explanation:
In the preceding illustration, the sort function has been utilized to arrange the specified list ([2, 9, 5, 20, 15]) in ascending sequence.
5. How do we copy a list without modifying the original?
Python includes a list method known as copy that allows you to duplicate a list while leaving the original unaltered. Below is a straightforward example demonstrating how to utilize the copy method.
Example
# creating a list
num_lst = [2, 9, 5, 20, 15]
# copying the list
new_lst = num_lst.copy()
# printing the new list
print(new_lst)
Output:
[2, 9, 5, 20, 15]
Explanation:
In the preceding example, we utilized the copy method to duplicate the specified list, which we then assigned to a new list.