Lesson 27 of 70 – Python List Methods
39%

Python List Methods

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.

Note: List methods are called using the dot . operator after the list name.
What are List Methods?

List methods are built-in functions provided by Python to perform different operations on lists.

numbers = [10, 20, 30]

numbers.append(40)

print(numbers)
Output:
[10, 20, 30, 40]
append()

The append() method adds one item to the end of a list.

numbers = [10, 20, 30]

numbers.append(40)

print(numbers)
Output:
[10, 20, 30, 40]
insert()

The insert() method adds an item at a specific position.

numbers = [10, 20, 30]

numbers.insert(1, 15)

print(numbers)
Output:
[10, 15, 20, 30]

Here, 1 is the index and 15 is the value to insert.

extend()

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)
Output:
[10, 20, 30, 40, 50]
remove()

The remove() method removes the first matching value from a list.

numbers = [10, 20, 30, 20]

numbers.remove(20)

print(numbers)
Output:
[10, 30, 20]

Only the first occurrence of 20 is removed.

pop()

The pop() method removes and returns an item from a list.

numbers = [10, 20, 30]

value = numbers.pop()

print(value)
print(numbers)
Output:
30
[10, 20]

Without an index, pop() removes the last element.

pop() with Index

An index can be passed to pop() to remove a specific element.

numbers = [10, 20, 30, 40]

numbers.pop(1)

print(numbers)
Output:
[10, 30, 40]
clear()

The clear() method removes all elements from a list.

numbers = [10, 20, 30]

numbers.clear()

print(numbers)
Output:
[]
index()

The index() method returns the index of the first matching value.

names = ["Amit", "Rahul", "Priya"]

print(names.index("Rahul"))
Output:
1
count()

The count() method returns the number of times a value appears in a list.

numbers = [10, 20, 10, 30, 10]

print(numbers.count(10))
Output:
3
sort()

The sort() method sorts a list in ascending order by default.

numbers = [40, 10, 30, 20]

numbers.sort()

print(numbers)
Output:
[10, 20, 30, 40]
sort(reverse=True)

The reverse=True argument sorts the list in descending order.

numbers = [10, 40, 20, 30]

numbers.sort(reverse=True)

print(numbers)
Output:
[40, 30, 20, 10]
reverse()

The reverse() method reverses the current order of list elements.

numbers = [10, 20, 30, 40]

numbers.reverse()

print(numbers)
Output:
[40, 30, 20, 10]
copy()

The copy() method creates a shallow copy of a list.

numbers = [10, 20, 30]

new_numbers = numbers.copy()

print(new_numbers)
Output:
[10, 20, 30]
Adding Multiple Items

The extend() method can add multiple elements at once.

fruits = ["Apple", "Banana"]

fruits.extend(["Mango", "Orange"])

print(fruits)
Output:
['Apple', 'Banana', 'Mango', 'Orange']
Working with Strings
names = ["Rahul", "Amit", "Priya"]

names.sort()

print(names)
Output:
['Amit', 'Priya', 'Rahul']

By default, strings are sorted in ascending lexicographical order.

remove() with Duplicate Values
numbers = [10, 20, 10, 30]

numbers.remove(10)

print(numbers)
Output:
[20, 10, 30]

Only the first matching 10 is removed.

Using index() and pop()
numbers = [10, 20, 30, 40]

position = numbers.index(30)

numbers.pop(position)

print(numbers)
Output:
[10, 20, 40]
List Methods Return Values

Many list methods modify the original list and return None.

numbers = [30, 10, 20]

result = numbers.sort()

print(numbers)
print(result)
Output:
[10, 20, 30]
None

The sort() method changes the list in place.

List Method Summary
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() vs extend()

append():

numbers = [1, 2]

numbers.append([3, 4])

print(numbers)
Output:
[1, 2, [3, 4]]

extend():

numbers = [1, 2]

numbers.extend([3, 4])

print(numbers)
Output:
[1, 2, 3, 4]
remove() vs pop()
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.
Important: Empty List Errors

Some list methods can raise an error if used incorrectly.

numbers = []

numbers.pop()
Result:
IndexError: pop from empty list

Always make sure the list contains an item before using pop() when necessary.

Methods vs Functions

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.

Practical Example
students = ["Rahul", "Amit"]

students.append("Priya")
students.insert(0, "Neha")
students.remove("Amit")
students.sort()

print(students)
Output:
['Neha', 'Priya', 'Rahul']

Multiple list methods can be used together to manage a list.

Key Points
  • 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.
  • sort() sorts the list.
  • reverse() reverses the list.
  • copy() creates a shallow copy.

🧠 Quick Quiz

Question: Which list method adds an item to the end of a list?