Python list methods are built-in functions for performing operations on lists. Here’s a comprehensive list of common list methods:
1. append() method
Adds an element to the end of the list.
Example:
fruits = ['apple', 'banana', 'orange']
fruits.append('mango')
print(fruits) // ['apple', 'banana', 'orange', 'mango']
2. extend() method
The extend()
method in Python is used to add all elements from an iterable (like a list, tuple, or string) to the end of an existing list. It’s different from append()
, which only adds a single item, because extend()
allows multiple items to be added.
Example:
fruits = ['apple', 'banana', 'orange']
more_fruits = ['mango', 'grape']
fruits.extend(more_fruits)
print(fruits) // ['apple', 'banana', 'orange', 'mango', 'grape']
3. insert() method
Inserts an element at a specified position.
fruits = ['apple', 'banana', 'orange']
fruits.insert(1, 'mango')
print(fruits) // ['apple', 'mango', 'banana', 'orange']
4. remove() method
The remove()
method in Python is used to delete the first occurrence of a specified value from a list. If the value is not found, it raises a ValueError
.
Syntax
list.remove(element)
element
: The value you want to remove from the list.
Example
fruits = ['apple', 'banana', 'orange']
fruits.remove('banana')
print(fruits) // ['apple','orange']
5. pop() method
The pop()
method in Python is used to remove and return an element from a specified index in a list. If no index is specified, it removes and returns the last element. If you try to pop()
from an empty list, it raises an IndexError
.
Syntax:
list.pop(index)
index
(optional): The position of the element to remove. If omitted, pop()
removes the last element.
Example1:
fruits = ['apple', 'banana', 'orange']
fruits.pop()
print(fruits) // ['apple','banana']
Example2:
fruits = ['apple', 'banana', 'orange']
fruits.pop(1)
print(fruits) // ['apple','orange']
6. clear() method
Removes all elements from the list.
fruits = ['apple', 'banana', 'orange']
fruits.clear()
print(fruits) // []
7. index() method
The index()
method in Python returns the index of the first occurrence of a specified element in a list. If the element is not found, it raises a ValueError
.
Syntax
list.index(element, start, end)
element
: The item you want to find in the list.
start
(optional): The position to start searching from (default is the beginning of the list).
end
(optional): The position to end the search (default is the end of the list).
Example:
fruits = ['apple', 'banana', 'orange']
fruits.index('banana')
print(fruits) // 1
8. count() method
The count()
method in Python returns the number of times a specified element appears in a list.
Syntax
list.count(element)
element
: The item you want to count in the list.
Example:
# Initial list
fruits = ['apple', 'banana', 'cherry', 'banana', 'orange', 'banana']
# Count occurrences of 'banana'
banana_count = fruits.count('banana')
print(banana_count) # Output: 3
9. sort() method
The sort()
method in Python sorts the elements of a list in ascending order by default. This method modifies the existing list and does not return a new one list. Optionally, you can sort the list in descending order by setting the reverse
parameter to True
.
Syntax
list.sort(key=None, reverse=False)
key
(optional): A function that serves as a key for the sort comparison, allowing for custom sorting.
reverse
(optional): A Boolean value. Set it to True
to sort the list in descending order.
Example:
# Initial list
numbers = [3, 1, 4, 1, 5, 9]
# Sort in ascending order (default)
numbers.sort()
print(numbers) # Output: [1, 1, 3, 4, 5, 9]
# Sort in descending order
numbers.sort(reverse=True)
print(numbers) # Output: [9, 5, 4, 3, 1, 1]
10. reverse() method
The reverse()
method in Python reverses the elements of a list in place, meaning it modifies the original list without creating a new one. This method does not sort the list but simply reverses the order of the items.
Syntax
list.reverse()
Example1:
# Initial list
numbers = [1, 2, 3, 4, 5]
# Reverse the list
numbers.reverse()
print(numbers) # Output: [5, 4, 3, 2, 1]
Example2:
fruits = ['apple', 'banana', 'orange']
fruits.reverse()
print(fruits) # Output: ['orange', 'banana', 'apple']
11 . copy() method
The copy()
method in Python creates a shallow copy of a list, meaning it generates a new list with references to the same elements as the original list. This method does not modify the original list.
Syntax
new_list = original_list.copy()
Example
# Original list
fruits = ['apple', 'banana', 'orange']
# Create a copy of the list
fruits_copy = fruits.copy()
print(fruits_copy) # Output: ['apple', 'banana', 'orange']
Modifying the Original List
If you modify the original list, the copied list remains unchanged, as they are separate lists.
# Modify the original list
fruits.append('mango')
print(fruits) # Output: ['apple', 'banana', 'orange', 'mango']
print(fruits_copy) # Output: ['apple', 'banana', 'orange']