HOLIDAY SALE! Save 50% on Membership with code HOLIDAY50. Save 15% on Mentorship with code HOLIDAY15.

2) More Data Types Lesson

Python List Methods

9 min to complete · By Martin Breuss

You've gotten to know string methods in a previous lesson. Many Python objects you'll work with have methods associated with them. You'll learn more about what methods are in an upcoming lesson. For now, you can think of a list method as a way to do something with your list.

Appending and Popping Elements

You can use list methods to change your list. For example, you can add elements to the end of your list and remove them as well:

bucket_list = ["climb Mt. Everest", "eat fruits from a tree that I planted"]

# Add an element
bucket_list.append("sail across the Atlantic ocean")
print(bucket_list)  

# Remove an element
bucket_list.pop()

# print bucket_list again
print(bucket_list)  

Using list.append(), you can add elements to the end of your list, and with list.pop(), you can remove the last item in your list.

In addition to removing the last element, list.pop() also returns the removed element, which means that you can assign it to a variable and keep using it in your program:

bucket_list = ["climb Mt. Everest", "eat fruits from a tree that I planted"]
next_task = bucket_list.pop()

print(next_task) 
# OUTPUT: "eat fruits from a tree that I planted"
print(bucket_list) 
# OUTPUT: ["climb Mt. Everest"]

You've successfully removed the last item from your bucket_list and assigned it to a new variable, next_task.

Removing and Inserting Items

Sometimes, however, you might want to remove a different item than the last one. For this, you can use a different list method called list.remove():

bucket_list = ["climb Mt. Everest", "eat fruits from a tree that I planted"]
bucket_list.remove("climb Mt. Everest")
print(bucket_list)  
# OUTPUT: ["eat fruits from a tree that I planted"]

When using list.remove(), you can specify the value of the element that you want to remove from your list. Keep in mind that you'll need to pass it the exact value of the item you want to take out. You can make sure to avoid any typos by indexing your bucket_list, which returns the value of the element:

bucket_list = ["climb Mt. Everest", "eat fruits from a tree that I planted"]
bucket_list.remove(bucket_list[0])
print(bucket_list)  
# OUTPUT: ["eat fruits from a tree that I planted"]

Using indexing to access the value of a list element and passing this value to .remove() allows you to target the item that you want to take out of your list effectively.

Practice Using list.remove()

  • Practice using list.remove() by creating a list a = [1, 2, 3, 2] and then removing the value 2.
  • What do you notice when you run this code?
  • Do all occurrences of 2 get removed?
  • If not, then which 2 has been removed? What direction does list.remove() operate in?
# your turn!

Inserting Elements

Similarly to removing elements from specific positions in your list, you can also insert them at specific positions. For that, you'll use list.insert(), and you'll need to specify both the index where you want to insert the new element as well as its value:

bucket_list = ["climb Mt. Everest", "eat fruits from a tree that I planted"]

# Add an element
bucket_list.insert(1, "sail across the Atlantic ocean")
print(bucket_list)  

You'll first add a number to specify where your new item should be inserted. In this case, you chose the number 1, which means the value will be inserted after the first position, which has the index of 0. The inserted element will be at the index you specify here.

The second argument to list.insert() is the value of the element that you want to add to your list. In the code snippet above, that is the string "sail across the Atlantic ocean". Run this example in your Python interpreter and confirm that you're getting the same result.

Sorting Lists

Another useful list method is list.sort(), which allows you to sort the items in your list. You can apply this to lists of strings, which will be sorted alphabetically by the beginning letters of your elements, or also to lists of numbers:

a = [3, 0, 999, 42]
a.sort()
print(a)  
# OUTPUT: [0, 3, 42, 999]

When you call .sort() on your list, the items in the list get sorted in place. This means that your list a has changed. This is only possible because lists are mutable. You can confirm that your list has been sorted by printing it out.

Applying Operators on Lists

Some of the Python operators that you covered earlier can also be used with lists. Revisit the previous lessons to refresh your knowledge of Python operators, then use them to get to know your new data type friend, list better.

Practice Operators and Lists

  • Try to apply Python operators such as +, *, /, etc. that you used with numbers and strings to lists. Which ones work and which produce errors?
  • Which operators do what you'd expect them to do?
  • Which operators work but produce unexpected results?
# your turn!

Many use cases of Python operators that apply to lists are intuitive and make working with lists quicker.

Colorful illustration of a light bulb

Additional Resources

Summary: Python List Methods

  • You can perform actions on list objects via list methods.
  • All of the methods below change the list that you are calling them on, which is possible because lists are mutable.
  • You can also apply some of the Python operators you've gotten to know earlier on lists.
  • list.append(item)
  • list.pop()
  • list.remove(item)
  • list.insert(index, item)
  • list.sort()