Python List Methods: A Practical Guide to Work with Lists

Lists are one of the main Python data types. They are very flexible because several Python list methods are available to perform operations on lists.

What list methods are available in Python?

Some examples of methods for Python lists are append (adds an item to the end of a list), insert (adds an item at a specific position in a list), remove (removes an item from a list), sort (sorts a list), index (returns the index of the first element with a given value in a list).

In this tutorial, you will go through multiple list methods to make sure you are comfortable when working with a Python list.

This tutorial is designed to be interactive, let’s get started!

What is a List Method in Python?

A method is a functionality provided by the Python list data type that can be called by specifying the name of the list, followed by a dot, and followed by the method you want to call.

When calling a method you usually pass an argument to it. In some cases, you can call a list method without passing any argument (e.g. with the pop() method).

Here is what the syntax to call a list method looks like:

list_name.method_name(argument)

Ready for some examples?

What Methods Does a List Support in Python?

The examples in this tutorial are based on a list of integers that contains the first seven numbers of the Fibonacci sequence.

Open the Python shell on your computer and create the list below. You will use it to execute the code examples in this tutorial:

fibonacci = [0,1,1,2,3,5,8]

Here is a table summarizing the most common methods supported by Python lists. This table will help you quickly identify list methods and remember how to use them.

To make this more interactive, open the Python shell, create the initial list as mentioned before, and then execute all the commands in the table, one row at a time.

The updated list you will get in your Python shell must match the “Updated list” in the “Syntax Examples” column of the table.

MethodDescriptionSyntax ExamplesNotes
appendAdds an item to the end of the listfibonacci.append(13)

Updated list:
[0, 1, 1, 2, 3, 5, 8, 13]
removeRemoves the first item from the list with the given valuefibonacci.remove(3)

Updated list:
[0, 1, 1, 2, 5, 8, 13]
Raises ValueError if the value is not found
insertInserts an item at a specified position in the listfibonacci.insert(4, 3)

Updated list:
[0, 1, 1, 2, 3, 5, 8, 13]
The first argument is the index, the second is the item to insert
popRemoves and returns the item at the given positionnumber = fibonacci.pop(1)

Updated list:
[0, 1, 2, 3, 5, 8, 13]
Without an index, it removes and returns the last item
extendAdds all elements from a list to the end of the current listfibonacci.extend([21, 34])

Updated list:
[0, 1, 2, 3, 5, 8, 13, 21, 34]
The argument must be an iterable. Raises a
TypeError if the argument is not an iterable.
sortSorts the list in ascending orderfibonacci.sort()

Updated list:
[0, 1, 2, 3, 5, 8, 13, 21, 34]
Can take arguments for key and reverse for customization
reverseReverses the elements of the list in placefibonacci.reverse()

Updated list:
[34, 21, 13, 8, 5, 3, 2, 1, 0]
indexReturns the index of the first element with the specified valuefibonacci.index(5)

Result:
4
Raises ValueError if the value is not found in the list

The following table shows some common functions you can use with Python lists. The result you will get in your Python shell must match the “Result” in the “Syntax Examples” column of the table.

Continue using the updated list you obtained after executing the reverse() method in the previous table:

[34, 21, 13, 8, 5, 3, 2, 1, 0]
FunctionDescriptionSyntax ExamplesNotes
len()Returns the number of items in the listprint(len(fibonacci))

Result:
9
This is not a method, it’s a built-in function
min()Returns the smallest item in the listmin(fibonacci)

Result:
0
This is not a method, it’s a built-in function
max()Returns the largest item in the listmax(fibonacci)

Result:
34
This is not a method, it’s a built-in function
sum()Returns the total sum of items in the listsum(fibonacci)

Result:
87
This is not a method, it’s a built-in function that works with numbers

There are more methods and functions available and you can find them in the official Python documentation.

Conclusion

In this tutorial, you have learned several list methods and functions available that will allow you to work with lists in your programs. You now know how to:

  • Add one element to the end of a list.
  • Remove an element from a list.
  • Add multiple elements to a list.
  • Sort and reverse the elements of a list.
  • Get the index of the first element with a specified value.
  • Calculate the number of elements in a list.
  • Get the smallest element, the largest element, and the sum of the elements in a list.

Let me know if you have any questions 🙂

Bonus read: now that you are more familiar with lists have a look at some simple exercises with Python lists.

5 thoughts on “Python List Methods: A Practical Guide to Work with Lists”

Leave a Comment