Concepts / Understanding sequences and mutability

Understanding sequences and mutability

A list is a sequence of values that can contain any data type. A string is a sequence of characters.

  • Programming

Two Kinds of Sequence

A sequence is an organized collection of values. In Python, a string is a sequence whose values are characters. A list is a sequence whose elements can be values of any data type. This difference makes lists suitable for collecting varied pieces of data, while strings represent text as characters.

Listmixed values5integerStringcharactersscharacterspamstringcan containcan containcontains
What is the difference between the kinds of values a list can contain and the characters a string can contain?
Key point

A list is a sequence of values that can contain any data type. A string is a sequence of characters.

Building Lists with Brackets

The simplest way to create a list is to place its elements between square brackets and separate the elements with commas. The brackets show where the list begins and ends. The commas separate one element from the next.

List formWhat it represents
[5, 8, 13, 21]A list of four integers
['red', 'blue', 'green']A list of three strings
[]An empty list with zero elements

Square brackets create lists; commas separate their elements.

Definition

An empty list is a valid list that contains zero elements. It is written with square brackets and nothing between them. Empty lists can serve as starting points for building lists as a program runs.

Worked example

Reading list structure

Consider the list [12, 'oak', 3.5]. What are its elements, and how many elements does it contain?

  1. 1
    Find the boundaries

    The square brackets show that the complete expression is one list.

  2. 2
    Separate the elements

    The commas separate 12, 'oak', and 3.5 into three individual elements.

  3. 3
    Identify the types

    The elements are an integer, a string, and a floating-point value. They do not need to have the same type.

Result

The list has three elements, and those elements have mixed data types.

Nested Lists and Containment

Because a list can contain any type of value, one of its elements can itself be another list. A list inside another list is called a nested list. The inner list remains a complete list; it is simply stored as one element of the outer list.

Outer listfour elementsspam102.0205Inner list[10, 20]Empty listzero elementscontainscontainscontainscontainscontainscontainscan contain
What contains what when a list contains another list, and where does an empty list fit into the structure?
In the real world

A nested list can organize data hierarchically. For example, an outer list might hold several values, while one element is a separate list containing related values. The outer list does not flatten the inner list; the inner list remains one element of the outer structure.

Names Referring to Lists

Lists can be assigned to variables just as numbers and strings can. After the assignment, the variable name refers to the entire list. This gives the list a convenient name that can be used to work with that list in a program.

scoresvariable nameList[10, 20, 30]refers to
When a list is assigned to a variable, how is the variable name related to the list data?

Changing a List Over Time

Lists are useful when the collection of values can grow while a program runs. You can begin with an empty list and add elements later. This is the practical connection between lists and mutability in this lesson: the list's contents can be built up over time, while the variable name continues to be the name used to work with the list.

itemsvariable nameitemsvariable name[]zero elements[value]an element has been addedrefers tocontents built over timerefers to
What changes in the list when contents are added, and what remains the same about the variable used to work with it?
Best practice

When you need a collection that will be filled as a program runs, start with an empty list. This makes the intended zero-element starting state explicit.

Mistakes Beginners Make

  • Common mistake

    Assuming every list element must have the same data type.

    What it looks like

    Treating a list containing an integer, a string, and a float as invalid.

    Why it fails

    Lists can contain mixed data types.

    Do this instead

    Check each element separately; integers, strings, floats, and other values can coexist in one list.

  • Common mistake

    Treating a nested list as if its values were automatically separate elements of the outer list.

    What it looks like

    Viewing [10, 20] inside an outer list as two outer elements.

    Why it fails

    The inner list is itself one complete element of the outer list.

    Do this instead

    Distinguish the outer list from the nested list it contains.

  • Common mistake

    Thinking square brackets with no contents do not form a list.

    What it looks like

    Treating [] as missing or incomplete data.

    Why it fails

    [] is a valid empty list with zero elements.

    Do this instead

    Recognize [] as a useful starting list that can receive elements later.

  • Common mistake

    Confusing a variable name with the list data.

    What it looks like

    Thinking the name items is an element inside the list it refers to.

    Why it fails

    The variable name refers to the entire list; it is not an element of that list.

    Do this instead

    Separate the name used to work with the list from the list's elements.

Check Your Understanding

MEDIUM

Describe the structure of this list: ['tea', 4, 1.5, [7, 8], []]. State how many elements the outer list has, identify the nested list, and identify the empty list.

Hints
  • Count the comma-separated elements at the outermost level.
  • A list inside the outer list counts as one outer element.
  • An empty list is still a list, even though it has zero elements of its own.

What do you think happens?

Before checking the explanation, predict whether the outer list in ['tea', 4, 1.5, [7, 8], []] has three, four, or five elements.

  • Three

  • Four

  • Five

Reveal answer
Answer:

Five

The outer list contains the string 'tea', the integer 4, the float 1.5, the nested list [7, 8], and the empty list []. Both inner lists count as individual elements of the outer list.

Key Takeaways

  1. 1
    A list is a sequence that can contain values of any data type, while a string contains characters.
  2. 2
    Square brackets create lists, commas separate elements, and [] creates an empty list.
  3. 3
    Lists may contain mixed data types and may contain other lists as nested elements.
  4. 4
    A variable assigned a list refers to the entire list, not to one extra element inside it.
  5. 5
    An empty list can be used as a starting point for building a collection as a program runs.

At a glance

  • Lists are sequences that can contain values of any data type; strings are sequences of characters.
  • Square brackets create lists, including empty lists and lists containing other lists.
  • A nested list is one complete element inside its outer list.
  • A variable name refers to the entire list assigned to it.
  • Lists can start empty and have their contents built as a program runs.

Related concepts

More concepts starting with U