A method is a functionality provided by the Python list data type that can be called by specifying the list followed by a dot and followed by the method you want to call.
Some examples of methods supported by 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).
When calling a method you usually pass an argument to it. Sometimes, you 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)
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 examples of code 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 fibonacci
list as mentioned before, and then execute all the commands in the table, one row at a time.
The updated list you will get from your Python shell must match the “Updated list” in the “Syntax Examples” column in the table.
Method | Description | Syntax Examples | Notes |
---|---|---|---|
append | Adds an item to the end of the list | fibonacci.append(13) Updated list: [0, 1, 1, 2, 3, 5, 8, 13] | – |
remove | Removes the first item from the list with the given value | fibonacci.remove(3) Updated list: [0, 1, 1, 2, 5, 8, 13] | Raises ValueError if the value is not found |
insert | Inserts an item at a specified position in the list | fibonacci.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 |
pop | Removes and returns the item at the given position | number = fibonacci.pop(1) Updated list: [0, 1, 2, 3, 5, 8, 13] | Without an index, it removes and returns the last item |
extend | Adds all elements from a list to the end of the current list | fibonacci.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. |
sort | Sorts the list in ascending order | fibonacci.sort() Updated list: [0, 1, 2, 3, 5, 8, 13, 21, 34] | Can take arguments for key and reverse for customization |
reverse | Reverses the elements of the list in place | fibonacci.reverse() Updated list: [34, 21, 13, 8, 5, 3, 2, 1, 0] | – |
index | Returns the index of the first element with the specified value | fibonacci.index(5) Result: 4 | Raises ValueError if the value is not found in the list |
count | Returns the number of times an element appears in the list | fibonacci.count(13) Result: 1 | Returns zero if the element is not in the list |
Useful Functions for Python Lists
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.
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]
Function | Description | Syntax Examples | Notes |
---|---|---|---|
len() | Returns the number of items in the list | print(len(fibonacci)) Result: 9 | This is not a method, it’s a built-in function |
min() | Returns the smallest item in the list | min(fibonacci) Result: 0 | This is not a method, it’s a built-in function |
max() | Returns the largest item in the list | max(fibonacci) Result: 34 | This is not a method, it’s a built-in function |
sum() | Returns the total sum of items in the list | sum(fibonacci) Result: 87 | This is not a method, it’s a built-in function that works with numbers |
More methods and functions are 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 look at some simple exercises with Python lists.
Claudio Sabato is an IT expert with over 15 years of professional experience in Python programming, Linux Systems Administration, Bash programming, and IT Systems Design. He is a professional certified by the Linux Professional Institute.
With a Master’s degree in Computer Science, he has a strong foundation in Software Engineering and a passion for robotics with Raspberry Pi.