Skip to Content
Nextra 4.0 is released 🎉

Lists

A list is a way to store multiple items in order—like a playlist of songs, a to-do list, or a row of lockers. You can add, change, and remove items, and each item has a position (called an index).

Items in a list can be numbers, strings, booleans, or even other lists. Most of the time you’ll use one type (e.g. all strings), but Python allows a mix if you need it.

Creating a List

You create a list by putting items inside square brackets [], with commas between them.

# Creating a list of integers numbers = [1, 2, 3, 4, 5] # Creating a list of strings fruits = ["apple", "banana", "cherry"] # Creating a mixed list mixed = [1, "apple", 3.14, True]

The mixed list shows that you can mix types in one list. In practice, you’ll usually keep a list to one type (all strings, or all numbers) so your code is easier to work with.

Creating an Empty List

You can create an empty list by using square brackets [] without any items inside.

empty_list = []

This is useful when you don’t know what items you’ll need to store in the list yet, or when you want to create a list that you’ll fill later.

Accessing List Items

You get an item from a list by using its position, or index. Use the list name followed by square brackets and the index: list_name[index].

The first item has index 0, the second has index 1, and so on. That can feel odd at first—you’ll get used to it!

fruits = ["apple", "banana", "cherry"] print(fruits[0]) # Output: apple print(fruits[1]) # Output: banana print(fruits[2]) # Output: cherry

You can also use negative indexes to count from the end: -1 is the last item, -2 is the second-to-last, and so on.

fruits = ["apple", "banana", "cherry"] print(fruits[-1]) # Output: cherry print(fruits[-2]) # Output: banana print(fruits[-3]) # Output: apple

Modifying List Items

You can change an item in a list by picking it with its index and giving it a new value—same idea as assigning to a variable.

fruits = ["apple", "banana", "cherry"] fruits[1] = "blueberry" # Changing "banana" to "blueberry" print(fruits) # Output: ['apple', 'blueberry', 'cherry']

List Methods

Lists come with built-in methods—actions you can run on that list, like “add an item” or “sort.” You use them by writing the list name, a dot, then the method name and parentheses (e.g. fruits.append("cherry")). Here are the ones you’ll use most:

  • append() — Adds an item to the end of the list.

    fruits = ["apple", "banana"] fruits.append("cherry") print(fruits) # Output: ['apple', 'banana', 'cherry']
  • remove() — Removes the first occurrence of that value from the list.

    fruits = ["apple", "banana", "cherry"] fruits.remove("banana") print(fruits) # Output: ['apple', 'cherry']
  • pop() — Removes an item and gives it back to you. With no argument, it removes and returns the last item.

    fruits = ["apple", "banana", "cherry"] last_fruit = fruits.pop() print(last_fruit) # Output: cherry print(fruits) # Output: ['apple', 'banana']
  • insert() — Puts an item at a specific index. Whatever was at that spot (and everything after it) shifts to the right.

    fruits = ["apple", "banana"] fruits.insert(1, "cherry") # "cherry" goes at index 1 print(fruits) # Output: ['apple', 'cherry', 'banana']

When you insert at an index, the item that was there (and all items after it) move right to make room.

  • sort() — Sorts the list in ascending order (smallest to largest, or A to Z for strings).

    numbers = [3, 1, 4, 2] numbers.sort() print(numbers) # Output: [1, 2, 3, 4]

To sort in reverse order, pass the argument reverse=True to sort().

numbers = [3, 1, 4, 2] numbers.sort(reverse=True) print(numbers) # Output: [4, 3, 2, 1]
  • len() — A built-in function (not a list method) that returns how many items are in the list.

    fruits = ["apple", "banana", "cherry"] print(len(fruits)) # Output: 3

The len() function is not a list method—it’s a built-in function. That means you don’t use the dot notation to call it. You call it like this: len(list_name).

These are the list tools you’ll use most often. For more, see the official Python documentation on lists .

Last updated on