Python List Methods

In Python, a method is a function that belongs to a specific class or object. It defines a behavior or action that an object of that class can perform.

List methods are predefined functions specially designed to perform operations on list objects. These methods allow you to efficiently manipulate, access, and modify list data.

Here is a table of all the methods of the list object:

MethodDescription
append(item)Adds a single item to the end of the list.
pop(index)Removes and returns the item at the given index (default: last item).
remove(item)Removes the first occurrence of the specified item.
extend(iterable)Adds all elements from the given iterable (e.g. another list) to the end of the list.
sort()Sorts the list in ascending order.
reverse()Reverses the order of the list in-place.
insert(index, item)Inserts an item at the specified index.
index(item)Returns the index of the first occurrence of the specified item.
count(item)Counts the number of times the specified item appears in the list.
clear()Removes all the items from the list, leaving it empty.
copy()Creates a shallow copy of the list.

Now, let’s take a look at each list method with a code example:

append()

The append() method is used to add a single item to the end of the list.

For example:

my_list = [5, 10, 15]

my_list.append(20)

print(my_list) # Output: [5, 10, 15, 20]

pop()

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

The index is optional. If no index is provided, it removes and returns the last item.

For example:

my_list = [5, 10, 15, 20]

popped_item = my_list.pop(0)

print(my_list) # Output: [10, 15, 20]

print(popped_item) # Output: 5

remove()

The remove() removes the first occurrence of a specified item from the list.

For example:

my_list = [5, 10, 15, 20]

my_list.remove(10)

print(my_list) # Output: [5, 15, 20]

extend()

The extend() method adds all the elements of an iterable (such as another list, tuple, or set) to the end of an existing list.

For example:

my_list = [5, 10, 15]

another_list = [100, 200, 300]

my_list.extend(another_list)

print(my_list) # Output: [5, 10, 15, 100, 200, 300]

sort()

The sort() method arranges the elements of a list in ascending or descending order. It modifies the list in place and doesn’t return a new list.

For example:

my_list = [5, 50, 10, 40 ]

# Sorting in ascending order
my_list.sort()

print(my_list) # Output: [5, 10, 40, 50]

# Sorting in descending order
my_list.sort(reverse=True)

print(my_list) # Output: [50, 40, 10, 5]

reverse()

The reverse() method reverses the order of the elements in a list in place, meaning it modifies the original list.

For example:

my_list = [5, 50, 10, 40 ]

my_list.reverse()

print(my_list) # Output: [40, 10, 50, 5]

insert()

The insert() method adds an element at a specified index in the list.

For example:

my_list = [5, 10, 15 ]

my_list.insert(1, 100)

print(my_list) # Output: [5, 100, 10, 15]

index()

The index() method is used to find the index (position) of the first occurrence of a specified item in a list.

For example:

my_list = [5, 10, 15, 10 ]

index = my_list.index(10)

print(index) # Output: 1

count()

The count() method is used to count the number of occurrences of a specified element in a list.

For example:

my_list = [5, 10, 15, 10 ]

count = my_list.count(10)

print(count) # Output: 2

clear()

The clear() method removes all elements from a list, leaving it empty.

For example:

my_list = [5, 10, 15, 20 ]

my_list.clear()

print(my_list) # Output: []

copy()

The copy() method creates a shallow copy of a list. A shallow copy means a new list object is created, but it contains references to the same elements as the original list.

For example:

my_list = [5, 10, 15, 20]

# Copying a list of numbers
new_list = my_list.copy()

# Modifying the new list doesn't modify the orignal list
new_list.append(30)

print(new_list) # Output: [5, 10, 15, 20, 30]

print(my_list) # Output: [5, 10, 15, 20]

However, if the list contains mutable objects, changes to other objects will be reflected in both lists.

For example:

# Orignal list with nested list
my_list = [5, [10, 15], 20]

# Copying a list of numbers
new_list = my_list.copy()

# Modifying the nested list in the new list
new_list[1][0] = 90

print(new_list) # Output: [5, [90, 15], 20]

print(my_list) # Output: [5, [90, 15], 20]

The copy() method creates a new list object but the nested list is still the same. Therefore, modifying the nested list affect both original and the copy.