Python List Collection Tutorial

In this tutorial we learn Python's most popular collection, the list. We cover how to declare a list, how to access, change and remove items, as well as list constructors and common list functions.

Here's a table of contents of what you'll learn in this lesson:
(click on a link to skip to its section)

Let's jump right in.

What is a list?

The list data type is one of the sequence, or collection, data types in Python. A list is a mutable collection of similar, or different, data types.

A list in Python is much like a shopping list we would make at home.

How to declare / initialize a list

To create a list in Python we initialize a variable and add values, separated by a comma, between square brackets.

Syntax:
 list_name = [item, item, item, ...]
Example: how to declare / initialize a list
 shopping_list = ["Bread", "Milk", "Cheese", "Apples"]

We can compare the example above to a normal shopping list we would make at home:

  1. Bread
  2. Milk
  3. Cheese
  4. Apples

It has a number, or index, and a value.

How to access list items

Python allows us to access list items in via loops or the indexer.

If we want to access single values in specific locations in the list, we use the indexer. If we want to access list items in bulk, we loop through the list.

How to access items in a list by index

An index is a number that corresponds to the item in our list. If we consider the shopping list again, it would look like this:

IndexItem
0Bread
1Milk
2Cheese
3Apples

A numerical index always starts at number 0, not at number 1.

To access an item in our list we specify the list name, followed by the index between open and close square brackets.

Syntax:
 list_name[1]
Example: access a list item through the index
shopping_list = ["Bread", "Milk", "Cheese", "Apples"]

print(shopping_list[0])
print(shopping_list[1])
print(shopping_list[2])
print(shopping_list[3])
How to access items in a list with a loop

In general, it will be easier, more efficient, and less code to access list items in a loop. A for loop is perfect for collections such as a list.

Example: access list items in a for loop
shopping_list = ["Bread", "Milk", "Cheese", "Apples"]

for item in shopping_list:
	print(item)

In the example above, our temporary variable is item. The loop then goes through the shopping_list items one by one and prints each item.

How to change a list item

List collections in Python are mutable, which means we can change the value of items at runtime.

To change a value, we specify the list name with its index between open and close square brackets, and assign a new value to it with the assignment operator.

Syntax:
list_name = [item_1, item_2, item_3, ...]

list_name[0] = new_value
Example: change a list item
shopping_list = ["Bread", "Milk", "Cheese", "Apples"]

shopping_list[0] = "Chocolate"

for item in shopping_list:
	print(item)

When we run the example above, we can see that the first item in the list has changed from “Bread” to “Chocolate”.

How to add an item to a list

Python provides us with two functions to add items to a list:

  • append()
  • insert()
How to append() an item to the end of a list

To add an item to the end of a list we use the append() function with dot notation.

The append() function is a function of the list class. To access a function on a class, we use dot notation. This means we write the name, followed by a dot ( . ), followed by the function.

Syntax:
list_name = [item_1, item_2, item_3, ...]

list_name.append(value)
Example: append() an item to the end of a list
shopping_list = ["Bread", "Milk", "Cheese", "Apples"]

shopping_list.append("Fish")

for item in shopping_list:
	print(item)

When we run the example above, we can see that “Fish” was added to the end of the list.

How to insert() item into specific location in a list

To add an item at a specific index we use a function called insert() . This function has two parameters, which means we need to write two values between the parentheses.

  1. The index we want to add the item to. The is value must be an integer.
  2. The value of the new item.
Syntax:
list_name = [item_1, item_2, item_3, ...]

list_name.insert(index, value)
Example: insert() an item in a specific location in a list
shopping_list = ["Bread", "Milk", "Cheese", "Apples"]

shopping_list.insert(3, "Eggs")

for item in shopping_list:
	print(item)

When we run the example above, we can see that “Eggs” was inserted into the list at index number 3. After “Cheese” and before “Apples”.

How to remove an item from a list

Python provides us with two functions to removes items from a list:

  • remove()
  • pop()
How to remove() an item by value

To remove an item from the list by its value, we use the remove() function with the value we want to remove as its parameter.

Syntax:
list_name = [item_1, item_2, item_3, ...]

list_name.remove(value)
Example: remove() an item by value
shopping_list = ["Bread", "Milk", "Cheese", "Apples"]

shopping_list.remove("Apples")

for item in shopping_list:
	print(item)

When we run the example above we can see that “Apples” has been removed from our list.

How to pop() an item out of the list by index

To remove an item from the list by its index, we use the pop() function with the index number we want to remove as its parameter.

Syntax:
list_name = [item_1, item_2, item_3, ...]

list_name.pop(index)
Example: pop() an item out of the list by index
shopping_list = ["Bread", "Milk", "Cheese", "Apples"]

shopping_list.pop(0)

for item in shopping_list:
	print(item)

When we run the example above, we can see that “Bread” was removed from the list because its index was 0.

If a parameter is not specified with the pop() function it will remove the item at the end of the list.

Example: pop() an item out of the end of a list
shopping_list = ["Bread", "Milk", "Cheese", "Apples"]

shopping_list.pop()

for item in shopping_list:
	print(item)

When we run the example above, we can see that “Apples” was removed from the list as it was the last item in the list.

How to check if item exists in list

We can check if an item exists in the list with an if statement and the in keyword.

Syntax:
list_name = [item_1, item_2, item_3, ...]

if item_1 in list_name:
	# do something
Example: check if an item exists in a list
shopping_list = ["Bread", "Milk", "Cheese", "Eggs"]

if "Milk" in shopping_list:
	print("Milk is in the shopping list")

The statement above reads: if the item Milk is in the shopping list, print a message.

How to clear all values from a list

Python allows us to clear a list completely without deleting the list itself. To do this we use the clear() function without any parameters.

Syntax:
list_name = [item_1, item_2, item_3, ...]

shopping_list.clear()
Example: clear all values in a list
shopping_list = ["Bread", "Milk", "Cheese", "Apples"]

shopping_list.clear()

for item in shopping_list:
	print(item)

When we run the example above, we won’t see anything printed because the list has been cleared.

How to delete a list completely

To delete a list completely we use the del keyword followed by the list name.

Syntax:
 del list_name
Example: delete a list completely
shopping_list = ["Bread", "Milk", "Cheese", "Apples"]

del shopping_list

for item in shopping_list:
	print(item)

When we run the example above we’ll see a NameError. The name “shopping_list” doesn’t exist anymore because we deleted the whole list.

How to create a list with the list constructor

Python allows us to make a list by using the list constructor.

Even though we learn about them here, constructors are a concept of classes. We learn more about classes in the object oriented programming lesson on classes and objects .

To create a list with the list constructor we use the keyword list , with the values we want to add between double parentheses.

Syntax:
 list_name = list((value_1, value_2, value_3, ...))
Example: create a list with the list constructor
shopping_list = list(("Bread", "Milk", "Cheese", "Apples"))

for item in shopping_list:
	print(item)

List Functions

The following table shows some common functions used with list:

FunctionDescription
append()Adds an element at the end of the list.
clear()Removes all the elements from the list.
copy()Returns a copy of the list.
count()Returns the number of elements with the specified value.
extend()Add the elements of a list (or any iterable), to the end of the current list.
index()Returns the index of the first element with the specified value.
insert()Adds an element at the specified position.
pop()Removes the element at the specified position.
remove()Removes the item with the specified value.
reverse()Reverses the order of the list.
sort()Sorts the list.

Summary: Points to remember

  • A list is like a variable that can store multiple separate values.
  • We store list values by writing them between square brackets, and separating each value with a comma.
  • We access list values with the indexer. That is to say, the index number that corresponds to the item’s location in the list.
  • Lists are mutable. We can change their values by assigning a new value to the index that corresponds to the item’s location in the list.
  • We add items to a list by using the append() or insert() functions.
  • We remove items from a list by using the remove() or pop() functions.
  • We check if an item exists in a list with an if/in statement.
  • We clear all values from a list with the clear function. This will only empty the list, not delete it entirely.
  • We delete a list entirely with the del keyword.