List methods in Python

Coding with Python

🕑 This lesson will take about 30 minutes

Methods are very much like functions. A method is something that you can do (an action) to a specific object in Python. The main difference between methods and functions is that in Python, functions can be independent of objects (eg. a function can be called to just perform a specific task without doing anything to an object) whereas methods are always used on objects.

When using methods, firstly you type the object’s name (what you want to carry out the action on), followed by a period character ( . ) and then the name of the method (the action) that you want to carry out on the object, followed by parentheses, including any required parameters listed inside the parentheses.

In this lesson, you will learn how to use the following methods on lists:

  • append()

  • count()

  • extend()

  • index()

  • insert()

  • pop()

  • remove()

  • reverse()

The append() method

In the example code below, we have a list of names. The append() method is used here to add another name to the list. After using the print() function to display the elements in list, we can see the extra name has been appended to the end of the list and the length of the list has grown.

The count() method

The count() method takes a list, and a value as an argument, and counts how many times the specified value occurs in the list. For example, we may have a list that contains several people’s names and we might want to find out how many times a particular name appears in that list.

Check out the example below. In this example, we have a list called names and in that list, there are several people’s names. Let’s say we want to find out how many people there are named “taylor” that exist in the list. We can use the count() method to do this. We would write names.count('taylor') and it would return an integer representing how many times the specified value occurs in the list. Remember, lists can contain data values of different data types, so this count() method can be used to count how many times an integer, float, string, or boolean value occurs in a list. In this example, we’re working with a list containing string values.

The extend() method

The extend() method is very similar to the append() method except that the append() method doesn’t allow you to append multiple values to a list, whereas the extend() function does. For example, we can extend an existing list by adding another list to it. Say that we make one list with the numbers 0 to 2 and a second list with the numbers 3 to 5. We want to extend the first list by adding the second list to it. We can use the extend method to do this.

To use the extend() method, we call it on a list and pass another list as an argument in the parentheses. Let’s say we have two lists called list_a and list_b. We want to add list_b to the end of list_a. The code syntax would look like list_a.extend(list_b) .

Check out the example below.

In the example below, we have a list containing names of the members of a team. The extend() method has been used to add some more names to the existing list.

The index() method

The index() method can be used to find the first occurrence of a specified value in a list and return its index (position in the list). For example, we can use the index() method to find the first instance of a particular word or number in a list. In the example code below, there is a list of random names. The index() method has been used to find the first occurrence of the name ‘alice’ . Notice that the string ‘alice’ occurs twice in the list, but the index() method will only return the index of the first instance. The first occurrence of ‘alice’ in the list is in index position 3, so the index() method returns 3 in this example.

The insert() method

The insert() method can be used to insert a new value into a list by specifying a value and the index (position) where it is to be inserted in a list. When we use the insert() method, Python inserts a new value in a specified position in a list, but it does not delete or replace the existing value in that position in the list. Instead, it inserts the new value in the specified position and shifts the existing value (and any values that come after it) further along in the list (moving those values to the right).

Check out the example code below. In this example, there is a list of names. A new name ("will") is being inserted into the list in index position 3 using the insert() method. The code names.insert(

The pop() and remove() methods

The pop() method is used to remove an element from a list. Using the pop() method, we can tell Python that we want to remove a value from a list by specifying the index (position) of the value we wish to remove from the list. For example, say we want to remove ‘maxʼ from the list of names in the example code below. The value ‘max’ has an index of 1 in the list, so we write names.pop(1) . Remember, if we don’t know the index of a value in a list, we can find the index of a desired value using the index method. Later on, we will be looking at if statements - an if statement could be used to check if a value existing in a list, then we could use the index() method to get the index of that value, and then the pop() method to remove the value from the list.

We can also use the remove() method to do the same job as the pop() method. However, using the remove() method, we can specify the value we wish to remove from a list rather than the index (position) of the value within the list. For example, let’s say we want to remove ‘alice’ from the list of names in the sample code below, we would just write names.remove('alice') . Note that if there are multiple occurrences of a specified value, only the first instance of that value in the list will be removed (in the first instance of using this method). In the example below, only the first instance of ‘alice’ will be removed from the list.

The reverse() method

The last method we will look at in this lesson is the reverse() method. The reverse() method takes a list and reverses the order of its contents. This method doesn’t require any parameters in the parentheses - all we need to do is call this method on a list and it will reverse the order of the contents of the list. We still need to use the () parentheses at the end of the method’s name but we donʼt need to put any information between them.

There are often several ways that a problem can be solved in programming. You might remember from earlier in this course that slicing can be used to reverse the contents of an array. For example, the code names = names[::-1] could also be used to achieve the same task as names.reverse() . Python offers a range of different methods that can be used to solve different problems whilst ensuring highly readable, succinct code.

Next lesson: Sorting lists and strings