Python List Methods
A list in Python is a collection of values that can be of any data type, including other lists. Lists are mutable, which means you can change their contents by adding, removing, or modifying elements. Python provides several built-in methods for manipulating lists.
Here are some of the most commonly used list methods in Python:
append()
The append() method adds an element to the end of a list.
fruits = ['apple', 'banana', 'orange'] # Add 'mango' to the end of the list fruits.append('mango') print(fruits) # ['apple', 'banana', 'orange', 'mango']
extend()
The extend() method adds all the elements of a given iterable (such as a list) to the end of the list.
fruits = ['apple', 'banana', 'orange'] # Add ['mango', 'pineapple'] to the end of the list fruits.extend(['mango', 'pineapple']) print(fruits) # ['apple', 'banana', 'orange', 'mango', 'pineapple']
insert()
The insert() method inserts an element at a given position in the list. It takes two arguments: the index of the position to insert the element, and the element itself.
fruits = ['apple', 'banana', 'orange'] # Insert 'mango' at position 1 fruits.insert(1, 'mango') print(fruits) # ['apple', 'mango', 'banana', 'orange']
remove()
The remove() method removes the first occurrence of a given element from the list. If the element is not found, it raises a ValueError exception.
fruits = ['apple', 'banana', 'orange', 'banana'] # Remove the first occurrence of 'banana' fruits.remove('banana') print(fruits) # ['apple', 'orange', 'banana']
pop()
The pop() method removes and returns the element at a given position in the list. If no index is specified, it removes and returns the last element.
fruits = ['apple', 'banana', 'orange'] # Remove and return the last element last_fruit = fruits.pop() print(last_fruit) # 'orange' print(fruits) # ['apple', 'banana'] # Remove and return the element at position 0 first_fruit = fruits.pop(0) print(first_fruit) # 'apple' print(fruits) # ['banana']
clear()
The clear() method removes all elements from the list.
fruits = ['apple', 'banana', 'orange'] # Remove all elements from the list fruits.clear() print(fruits) # []
index()
The index() method returns the index of the first occurrence of a given element in the list. If the element is not found, it raises a ValueError exception.
fruits = ['apple', 'banana', 'orange', 'banana'] # Get the index of the first occurrence of 'banana' banana_index = fruits.index('banana') print(banana_index) # 1 # Get the index of the first occurrence of 'mango' try: mango_index = fruits.index('mango') except ValueError: print('Mango not found') # Mango not found
count()
The count() method returns the number of occurrences of a given element in the list.
fruits = ['apple', 'banana', 'orange', 'banana'] # Count the number of occurrences of 'banana' banana_count = fruits.count('banana') print(banana_count) # 2 # Count the number of occurrences of 'mango' mango_count = fruits.count('mango') print(mango_count) # 0
sort()
The sort() method sorts the elements of the list in ascending order. It can take an optional key argument, which is a function that takes an element as input and returns a value that is used to determine the sort order.
numbers = [3, 1, 4, 2] # Sort the list in ascending order numbers.sort() print(numbers) # [1, 2, 3, 4] # Sort the list in descending order numbers.sort(reverse=True) print(numbers) # [4, 3, 2, 1] # Sort the list by length of the elements (ascending) fruits = ['apple', 'banana', 'orange'] fruits.sort(key=len) print(fruits) # ['apple', 'banana', 'orange']
reverse()
The reverse() method reverses the elements of the list in place.
numbers = [1, 2, 3, 4] # Reverse the list numbers.reverse() print(numbers) # [4, 3, 2, 1]
copy()
The copy() method returns a shallow copy of the list. A shallow copy is a new list with the same elements as the original list, but the elements themselves are not copied. If the elements are mutable (like lists), changes to the elements in the original list will be reflected in the copy.
original = [[1, 2], 3, 4] copy = original.copy() # Modify the first element of the original list original[0][0] = 9 print(original) # [[9, 2], 3, 4] print(copy) # [[9, 2], 3, 4]