Concepts / Understanding sequences and mutability

Understanding sequences and mutability

A list is a sequence of values that can contain any data type, unlike strings which contain only 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.

can containcan containcontainsListmixed values5integerStringcharactersscharacterspamstring
What is the difference between the kinds of values a list can contain and the characters a string can contain?

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.

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.

Reading list structure

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

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

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

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

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.

containscontainscontainscontainscontainscontainscan containOuter listfour elementsspam102.0205Inner list[10, 20]Empty listzero elements
What contains what when a list contains another list, and where does an empty list fit into the structure?

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.

refers toscoresvariable nameList[10, 20, 30]
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.

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

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

  • Assuming every list element must have the same data type.

    Lists can contain mixed data types.

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

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

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

    Fix: Distinguish the outer list from the nested list it contains.

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

    [] is a valid empty list with zero elements.

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

  • Confusing a variable name with the list data.

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

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

Key Takeaways

  • 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.