Skip to main content

List Methods

There are different methods to handle lists in Python.

Since list is probably one of the most used data types in Python, there are many usefull methods to know.

Adding Items (List Methods)

insert()

Inserting is when you add an item to a list at a specific index.

list = [1, 2, 3, 4, 5]
list.insert(0, 0)
print(list)

append()

Appending is when you add an item to the end of a list.

list = [1, 2, 3, 4, 5]
list.append(6)
print(list)

Removing Items (List Methods)

remove()

Remove is used to remove specific values from a list.

list = [1, 2, 3, 4, 5]
list.remove(3)
del list[-1]
print(list)

You can also use the del keyword to remove an item from a list.

pop()

Popping means removing the last item from a list.

list = [1, 2, 3, 4, 5]
list.pop()
print(list)

clear()

Clear is used to remove all items from a list.

list = [1, 2, 3, 4, 5]
list.clear()
print(list)

Getting info about each items

index()

index() is used to find the index of a value in a list.

list = [1, 2, 3, 4, 5, 1, 2, 3, 4, 5]
print(list.index(1))
print(list.index(2))
print(list.index(3))

count()

count() is used to count the number of times a value appears in a list.

list = [1, 2, 3, 4, 5, 1, 2, 3, 4, 5]
print(list.count(1))
print(list.count(2))
print(list.count(3))

Number List Methods

There are methods that only work in lists of numbers.

sum()

sum() is used to find the sum of all values in a list.

list = [1, 2, 3, 4, 5]
print(sum(list))

max() and min()

max() is used to find the largest value in a list.

min() is used to find the smallest value in a list.

list = [1, 2, 3, 4, 5]
print(max(list))
print(min(list))

Sorting Lists

sort()

sort() is used to sort a list in ascending order.

list = [5, 4, 3, 2, 1]
list.sort()
print(list)

reverse()

reverse() is used to reverse a list.

list = [5, 4, 3, 2, 1]
list.reverse()
print(list)

Combining Lists

extend

extend() is used to combine two lists.

list1 = [1, 2, 3, 4, 5]
list2 = [6, 7, 8, 9, 10]
list1.extend(list2)
print(list1)