Python online course

Python Lists: Creating, Indexing, and Slicing Lists

Learn how to create Python lists, access items with positive and negative indexes, select ranges with slicing, and modify list values.

A list is a mutable, ordered Python collection that stores multiple values under one variable name. Each stored value is an element, also called an item.

Lists are similar in purpose to arrays in some other programming languages: they let you keep related values together. However, a Python list is its own built-in collection type with Python-specific behavior.

Because a list is ordered, every element has a position. You can retrieve an element by using its numeric index.

Creating a Python list

List literals use square brackets: [ and ]. Separate elements with commas.

my_numbers = [5, 3, 2, 7]

In this statement:

  • my_numbers is the variable name.
  • [5, 3, 2, 7] is the list object assigned to the variable.
  • 5, 3, 2, and 7 are individual elements.

A list can contain numbers, strings, or other Python values. For example, colors = ["red", "green", "blue"] stores three strings.

Zero-based indexing

Python uses zero-based indexing. This means the first element has index 0, not index 1. An index is a numeric position used to access an item in a sequence.

my_numbers = [5, 3, 2, 7]

print(my_numbers[0])
print(my_numbers[1])
5
3

People commonly describe 5 as the first item and 3 as the second item. Python refers to those same positions as index 0 and index 1.

Indexes for an example list

Item position | Positive index | Negative index | Value

First | 0 | -4 | 5

Second | 1 | -3 | 3

Third | 2 | -2 | 2

Fourth | 3 | -1 | 7

Use square brackets after the list variable to perform direct indexing. Direct indexing returns one element, not a list:

my_numbers[0]    # 5
my_numbers[2]    # 2

Negative indexes

A negative index counts backward from the end of a list. The index -1 means the final element, -2 means the second-to-last element, and so on.

my_numbers = [5, 3, 2, 7]
print(my_numbers[-1])
print(my_numbers[-2])
7
2

-1 refers to the last item because negative indexing begins at the end and counts backward. It does not mean the first item. In this list, index -1 and positive index 3 refer to the same value: 7.

List slicing

A slice selects a portion of a list. The basic slice format is:

list[start:stop]

The start index is included in the result. The stop index is an exclusive boundary, so the item at that index is not included.

Omitting the start index

If the start index is omitted, the slice begins at the start of the list.

my_numbers = [5, 3, 2, 7]
print(my_numbers[:2])
[5, 3]

This includes indexes 0 and 1. Index 2 is the stop boundary, so its value, 2, is excluded.

Using both slice bounds

my_numbers = [5, 3, 2, 7]
print(my_numbers[1:3])
[3, 2]

The slice starts at index 1 and stops before index 3. Therefore, it includes the values at indexes 1 and 2, but not the value at index 3.

Indexing versus slicing

Expression | Operation type | Result | Explanation

my_numbers[0] | Indexing | 5 | Returns one element at index 0.

my_numbers[-1] | Negative indexing | 7 | Returns the final element.

my_numbers[:2] | Slicing | [5, 3] | Returns a new list from the beginning through index 1.

my_numbers[1:3] | Slicing | [3, 2] | Returns a new list containing indexes 1 and 2.

Direct indexing uses one index and returns one element. Slicing uses a colon and returns a new list that may contain zero or more elements.

Lists are mutable

Mutable means that an object can be changed after it is created. Lists are mutable, so you can replace an existing element by assigning a new value through its index.

my_numbers = [5, 3, 2, 7]
my_numbers[2] = 10
print(my_numbers)
[5, 3, 10, 7]

The assignment changes the element at index 2, replacing 2 with 10. The other elements remain unchanged.

Adding and removing items

Common list methods can add or remove elements:

my_numbers = [5, 3, 2, 7]
my_numbers.append(9)   # Adds 9 at the end
my_numbers.remove(3)   # Removes the element with value 3
print(my_numbers)
[5, 2, 7, 9]

append() adds one item to the end. remove() removes the first matching value. More list operations are covered in modifying lists.

Common list mistakes

Using index 1 for the first element

Python indexes begin at zero. Use my_numbers[0] for the first element. Using my_numbers[1] retrieves the second element.

Including the stop boundary in a slice

The stop index is exclusive. In my_numbers[1:3], index 3 is not included. To include an intended ending item at index 3, use my_numbers[1:4].

Using an invalid index

An index must refer to an existing position. For my_numbers = [5, 3, 2, 7], valid positive indexes are 0 through 3, and valid negative indexes are -1 through -4.

my_numbers = [5, 3, 2, 7]
print(my_numbers[4])

This raises IndexError because index 4 does not exist. Check the list's valid range before using an index. A later lesson can show how to handle exceptions with try and except.

Confusing an element with a one-item slice

my_numbers[1] returns the value 3. A slice such as my_numbers[1:2] returns the list [3]. Use indexing when you need one value and slicing when you need a list result.

Missing brackets or commas

A list literal requires square brackets and comma-separated elements:

my_numbers = [5, 3, 2, 7]

The general form is variable_name = [item1, item2, item3].

Key points

  • A list stores an ordered collection of values under one variable name.
  • List literals use square brackets, with elements separated by commas.
  • Python uses zero-based indexing, so the first element is at index 0.
  • Negative indexes count backward, with -1 representing the final element.
  • Slices use list[start:stop]; the start is included and the stop is excluded.
  • Indexing returns one element, while slicing returns a new list.
  • Lists are mutable and can be changed through item assignment and list methods.

After learning list access, you can practice looping through list elements with a Python for loop or using range().