Python provides several built-in methods to work with lists. These methods allow us to add, remove, search, sort, copy, and modify list elements easily.
List methods are built-in functions provided by Python to perform different operations on lists.
numbers = [10, 20, 30]
numbers.append(40)
print(numbers)
The append() method adds one item to the end of a list.
numbers = [10, 20, 30]
numbers.append(40)
print(numbers)
The insert() method adds an item at a specific position.
numbers = [10, 20, 30]
numbers.insert(1, 15)
print(numbers)
Here, 1 is the index and 15 is the value to insert.
The extend() method adds all elements of another iterable to the end of a list.
numbers = [10, 20]
numbers.extend([30, 40, 50])
print(numbers)
The remove() method removes the first matching value from a list.
numbers = [10, 20, 30, 20]
numbers.remove(20)
print(numbers)
Only the first occurrence of 20 is removed.
The pop() method removes and returns an item from a list.
numbers = [10, 20, 30]
value = numbers.pop()
print(value)
print(numbers)
Without an index, pop() removes the last element.
An index can be passed to pop() to remove a specific element.
numbers = [10, 20, 30, 40]
numbers.pop(1)
print(numbers)
The clear() method removes all elements from a list.
numbers = [10, 20, 30]
numbers.clear()
print(numbers)
The index() method returns the index of the first matching value.
names = ["Amit", "Rahul", "Priya"]
print(names.index("Rahul"))
The count() method returns the number of times a value appears in a list.
numbers = [10, 20, 10, 30, 10]
print(numbers.count(10))
The sort() method sorts a list in ascending order by default.
numbers = [40, 10, 30, 20]
numbers.sort()
print(numbers)
The reverse=True argument sorts the list in descending order.
numbers = [10, 40, 20, 30]
numbers.sort(reverse=True)
print(numbers)
The reverse() method reverses the current order of list elements.
numbers = [10, 20, 30, 40]
numbers.reverse()
print(numbers)
The copy() method creates a shallow copy of a list.
numbers = [10, 20, 30]
new_numbers = numbers.copy()
print(new_numbers)
The extend() method can add multiple elements at once.
fruits = ["Apple", "Banana"]
fruits.extend(["Mango", "Orange"])
print(fruits)
names = ["Rahul", "Amit", "Priya"]
names.sort()
print(names)
By default, strings are sorted in ascending lexicographical order.
numbers = [10, 20, 10, 30]
numbers.remove(10)
print(numbers)
Only the first matching 10 is removed.
numbers = [10, 20, 30, 40]
position = numbers.index(30)
numbers.pop(position)
print(numbers)
Many list methods modify the original list and return None.
numbers = [30, 10, 20]
result = numbers.sort()
print(numbers)
print(result)
The sort() method changes the list in place.
| Method | Purpose |
|---|---|
| append() | Adds an item at the end. |
| insert() | Adds an item at a specific index. |
| extend() | Adds multiple items. |
| remove() | Removes the first matching value. |
| pop() | Removes and returns an item. |
| clear() | Removes all items. |
| index() | Returns the index of a value. |
| count() | Counts occurrences of a value. |
| sort() | Sorts the list. |
| reverse() | Reverses the list. |
| copy() | Creates a shallow copy. |
append():
numbers = [1, 2]
numbers.append([3, 4])
print(numbers)
extend():
numbers = [1, 2]
numbers.extend([3, 4])
print(numbers)
| remove() | pop() |
|---|---|
| Removes a value. | Removes an item using its index. |
| Does not return the removed value. | Returns the removed value. |
| Removes the first matching value. | Without an index, removes the last item. |
Some list methods can raise an error if used incorrectly.
numbers = []
numbers.pop()
Always make sure the list contains an item before using pop() when necessary.
List method:
numbers.append(10)
Built-in function:
len(numbers)
Methods are called using the list object and dot operator, while functions such as len() are called directly.
students = ["Rahul", "Amit"]
students.append("Priya")
students.insert(0, "Neha")
students.remove("Amit")
students.sort()
print(students)
Multiple list methods can be used together to manage a list.
Question: Which list method adds an item to the end of a list?