Concepts / Accessing list elements with indexing

Accessing list elements with indexing

A list is a sequence of values that can contain any data type, unlike strings which contain only characters.

  • Programming
Interactive lab

Try it: Indexing and Slicing

How Python indexes a sequence from 0 (or from the end with negative numbers) and how a slice start:stop:step picks positions — with the exact CPython rules for omitted and out-of-range bounds.

How it works

  1. Positions run 0 … len−1; negative positions count back from the end (−1 is the last).
  2. An index outside the string raises IndexError.
  3. A slice resolves start and stop (omitted → the ends; out of range → clamped), then walks by step, stopping before stop.
  4. A negative step walks backwards; a step of 0 is a ValueError.

Default run (7 steps): 'PYTHONIC'[1:6:2]: a slice copies characters from start up to (not including) stop, moving by step. … 'PYTHONIC'[1:6:2] → 'YHN' (positions 1, 3, 5).

Simplified: Works on a short string (up to 16 printable ASCII characters); lists slice by exactly the same rules.

Educational simulation

Loading the simulation…

From Sequence to Element

A list is a sequence of values. Because the values have positions in that sequence, you can access an individual element by using its index. The important starting point is to recognize that a list is not one single value in the same sense as a string: it is a container holding separate elements.

Definition

A list is a sequence of values that can contain any data type. A string, by contrast, contains only characters.

0indexredelement1indexgreenelement2indexblueelementselectsselectsselects
How does each index position map to a specific element in a list?

Reading Positions

Indexing connects a position number to the value stored at that position. In Python, the first position is index 0, the next position is index 1, and the positions continue upward through the list. Therefore, the index is not the value itself; it is the position used to select the value.

Worked example

Selecting an Element by Position

Consider the list ['red', 'green', 'blue']. Which element is selected by index 1?

  1. 1
    Identify the positions

    The list is a sequence, so its elements have ordered positions. Counting from 0 gives red at index 0, green at index 1, and blue at index 2.

  2. 2
    Use the requested index

    Index 1 refers to the second element in the sequence.

  3. 3
    Read the element

    The value at index 1 is green.

Result

Index 1 selects the element green.

Lists and Strings

Value typeWhat it containsHow its elements are represented
ListValues that can have any data typeSeparate elements, written between square brackets and separated by commas
StringCharacters onlyCharacters forming the string

Both lists and strings are sequences, but the kinds of values they contain differ.

Listmixed values5elementscharacterspamelementStringcharacters onlycontainscontainscontains
What is the difference between the elements represented in a list and the characters represented in a string?

The difference matters when interpreting an index. An index into a list selects one value from the list, and that value might be an integer, a string, a float, or another list. An index into a string selects a character because strings contain only characters.

Building List Structures

Square brackets create lists. Write elements inside the brackets and separate them with commas. The brackets can contain several values, no values, or another list.

  • A list of integers can contain four integer elements.
  • A list of strings can contain three string elements.
  • A mixed list can contain integers, strings, floats, and other types together.
  • An empty list is written as [] and contains zero elements.
  • A nested list is a list contained as an element inside another list.
outer listfour elementsspamstring10integer2.0float20integer5integerinner listlist elementcontainscontainscontainscontainscontainscontains
What contains what when one list includes values of different types and another list?
Worked example

Reading a Nested Element

Consider the mixed list containing the string spam, the float 2.0, the integer 5, and the nested list [10, 20]. What is the fourth element of the outer list?

  1. 1
    Count the outer elements

    The outer list has four elements: spam, 2.0, 5, and [10, 20].

  2. 2
    Separate the levels

    The list [10, 20] is one complete element of the outer list. Its two integers belong to the inner list.

  3. 3
    Apply the outer position

    The fourth outer element is at index 3 because the first outer element has index 0.

Result

The fourth element of the outer list is the nested list [10, 20].

Naming the List

A list can be assigned to a variable just as a number or string can. The variable name refers to the entire list, not automatically to one particular element. After assigning a list to a variable, use that name when working with the list and use an index when selecting one of its elements.

colorsvariable namelistred, green, blueredelementgreenelementblueelementrefers tocontainscontainscontains
How is a variable name connected to the list data it refers to?

Mistakes with Positions

  • Common mistake

    Treating the first element as index 1

    What it looks like

    For ['red', 'green', 'blue'], treating red as index 1

    Why it fails

    Python list indexing starts at 0, so the first element has index 0.

    Do this instead

    Map the first element to 0, the second to 1, and continue upward.

  • Common mistake

    Assuming every list element must have the same data type

    What it looks like

    Assuming a list cannot contain both 5 and 'spam'

    Why it fails

    Lists can contain mixed data types, including integers, strings, floats, and other types.

    Do this instead

    Identify each element by its own value and data type.

  • Common mistake

    Counting the contents of a nested list as separate outer elements

    What it looks like

    Treating [10, 20] as two outer elements instead of one nested-list element

    Why it fails

    A nested list is itself one complete element of the outer list.

    Do this instead

    Count the nested list once when identifying outer positions, then inspect its contents separately.

  • Common mistake

    Confusing a variable name with an individual element

    What it looks like

    Assuming colors refers only to the first value in a list

    Why it fails

    A variable assigned to a list refers to the entire list.

    Do this instead

    Use an index after the variable name when you need one particular element.

Check Your Indexing

MEDIUM

A list contains the values ['tea', 7, 3.5, ['hot', 'cold']]. Identify the number of elements in the outer list, state the index of 3.5, and explain whether ['hot', 'cold'] is one outer element or two.

Hints
  • Count the outer values without opening the nested list.
  • Remember that the first outer element has index 0.
  • The nested list is itself an element of the outer list.

What do you think happens?

In the list ['tea', 7, 3.5, ['hot', 'cold']], what does index 2 select?

  • tea

  • 7

  • 3.5

  • ['hot', 'cold']

Reveal answer
Answer:

3.5

Counting from index 0 gives tea at index 0, 7 at index 1, 3.5 at index 2, and the nested list at index 3.

Key Takeaways

  1. 1
    A list is a sequence of values and can contain any data type, while a string contains only characters.
  2. 2
    Square brackets create lists; [] creates a valid empty list.
  3. 3
    Lists can contain mixed values and can contain other lists as nested elements.
  4. 4
    A variable assigned to a list refers to the entire list.
  5. 5
    Indexing selects an element by position, with the first position represented by index 0.

At a glance

  • Lists are sequences whose elements can have different data types.
  • Square brackets create lists, including empty and nested lists.
  • A variable name can refer to an entire list rather than one element.
  • Indexing uses a position to select one element, and the first position is index 0.
  • A nested list counts as one element of its outer list.

Related concepts

More concepts starting with A