Concepts / Accessing List Elements by Index

Accessing List Elements by Index

Reassigning an element with bracket notation changes the list in place. Any other name that refers to that same list will see the change as well.

  • Programming

A List That Changes After Creation

Most of the time when you first meet a list, you build it once and then read from it. But a list does not have to stay the way you created it. Lists are mutable, which means you can change their contents after creation. The change is made by reassigning an element using bracket notation on the left side of an assignment.

What do you think happens?

You create colors = ['red', 'green', 'blue'], then run colors[1] = 'yellow' and print(colors). What do you expect to see?

  • ['red', 'green', 'blue']

  • ['red', 'yellow', 'blue']

  • ['yellow', 'green', 'blue']

  • An error, because lists cannot be changed

Reveal answer
Answer:

['red', 'yellow', 'blue']

The assignment colors[1] = 'yellow' replaces the element at position 1, which was 'green'. Everything else in the list is untouched, and the list is still the same list with the same name.

Reading Positions Through the Index Map

A list represents a mapping between indices and elements. Each index position corresponds to exactly one element. An index is the position number you write inside the square brackets, and it is what tells Python which slot you mean. In Python, the first position is index 0, the second is index 1, and so on. So a list with three elements has indices 0, 1, and 2, and each of those numbers points to exactly one element.

appleindex 0bananaindex 1cherryindex 2
Which index number holds which element? Each slot in the list is labeled with its index, and each index maps to exactly one element.

Counting from 0 means the index is always one less than the element's position when you count from 1. The first element is at index 0, so to get it you write the index 0, not 1. Keep this in mind whenever you read an element or write to one.

python
Output (expected)
apple
cherry
Notes on the code
  • fruits[0] reads the element at index 0, which is 'apple'
  • fruits[2] reads the element at index 2, which is 'cherry'

Tracing a Reassignment Step by Step

Reading an element and changing an element use the same bracket notation. The difference is position. When the bracket expression sits on the left side of an equals sign, Python treats it as a target to store a new value into. Here is the same list before and after one reassignment.

Before
fruits = ['apple', 'banana', 'cherry']
print(fruits)

The list holds apple at index 0, banana at index 1, and cherry at index 2.

After
fruits = ['apple', 'banana', 'cherry']
fruits[1] = 'blueberry'
print(fruits)

The slot at index 1 now holds blueberry. The old value banana is gone from the list, and apple and cherry are unchanged.

appleindex 0appleindex 0bananaindex 1blueberryindex 1cherryindex 2cherryindex 2
After running fruits[1] = 'blueberry', which slot changed, and what does the rest of the list look like? Only the slot at index 1 changes.
Worked example

Changing one element and reading the result

Start with scores = [70, 85, 90]. Change the element at index 2 to 95, then print the whole list.

  1. 1
    Start with the list

    scores holds 70 at index 0, 85 at index 1, and 90 at index 2.

  2. 2
    Write the reassignment

    Put the bracket expression on the left side: scores[2] = 95. This targets the slot at index 2.

  3. 3
    Check the other slots

    Indices 0 and 1 were not named in the assignment, so 70 and 85 stay exactly where they were.

  4. 4
    Print the list

    print(scores) shows the updated contents of the same list.

Result

The output is [70, 85, 95]. Only the element at index 2 changed.

Why Reassignment Changes the Same List

A key point about reassignment is that it modifies the existing list in place. Python does not build a new list when you write fruits[1] = 'blueberry'. The variable fruits still refers to the same list it referred to before, and only the contents of that list have been altered.

fruitsvariable namelist objectthree elements, index 1changedrefers to
When the list is modified, does the variable still refer to the same list object, or does Python create a new one? The variable keeps pointing at the same list object, whose contents have changed.
Key point

Reassigning an element with bracket notation changes the list in place. Any other name that refers to that same list will see the change as well.

python
Output (expected)
['avocado', 'banana', 'cherry']
Notes on the code
  • alias is a second name for the same list, not a copy
  • The change made through fruits is visible through alias, because both names refer to one list

Checking Membership with the in Operator

Sometimes you do not care where an element is. You only want to know whether a value is present at all. The in operator answers that question. It checks for membership in a list and returns True if the value exists somewhere in the list, and False otherwise. The in operator looks at the values, not at the index numbers, so it does not tell you which position holds the value.

python
Output (expected)
True
False
False
Notes on the code
  • 'banana' is present at index 1, so the check returns True
  • 'mango' is not in the list, so the check returns False
  • 1 is an index, not a value stored in the list, so the check returns False

Mistakes When Working With Indices

  • Common mistake

    Counting positions from 1 instead of 0

    What it looks like

    fruits[1] is expected to give 'apple' in a list whose first element is 'apple'.

    Why it fails

    Indices start at 0, so 'apple' is at index 0 and index 1 holds 'banana'.

    Do this instead

    Write the first element as index 0, the second as index 1, and so on.

  • Common mistake

    Thinking reassignment creates a new list

    What it looks like

    After fruits[1] = 'blueberry', assuming fruits is now a fresh object unrelated to earlier references.

    Why it fails

    Reassignment modifies the existing list in place, so every name referring to that list sees the change.

    Do this instead

    Expect the same list to change, and check for other names that share it before assuming it is independent.

  • Common mistake

    Using the in operator to find which index holds a value

    What it looks like

    Writing if 'banana' in fruits and then assuming the position is 1 without checking.

    Why it fails

    The in operator only returns True or False for membership. It does not report the index.

    Do this instead

    Use the membership check to confirm presence, and read the element by index when you need the position.

  • Common mistake

    Putting the bracket expression on the wrong side of the equals sign

    What it looks like

    Writing 'blueberry' = fruits[1] when you intend to change the element.

    Why it fails

    Bracket notation must be on the left side of the assignment to store a new value into that slot.

    Do this instead

    Write fruits[1] = 'blueberry' so the slot at index 1 is the target.

Trace the Changes Before Running

MEDIUM

Without running the code, predict the output of this program: letters = ['a', 'b', 'c', 'd']; letters[0] = 'z'; letters[3] = 'y'; print(letters); print('b' in letters); print('q' in letters). Then run it to check your trace.

Hints
  • Each reassignment replaces exactly one slot. Find the index of each slot before you change it.
  • Index 0 is the first element and index 3 is the fourth element.
  • The in operator returns True only when the value is somewhere in the list right now, after the reassignments.

Key Takeaways

  • Lists are mutable, so you can change their contents after creation by reassigning an element with bracket notation on the left side of an assignment.
  • A list maps each index to exactly one element, and counting starts at index 0.
  • Reassignment modifies the existing list in place rather than creating a new list, so every name that refers to that list sees the change.
  • The in operator checks whether a value exists anywhere in the list and returns True or False, without reporting its index.

Related concepts

More concepts starting with A