Lists - Intro to the Data Structure & Common Operations

Lists are mutable sequence types to store a collection of items. Mutable is the ability to alter the items in place without re-creating a new list.

Python lists can be collections of items you have on your computer such as:

  • browser bookmarks
  • songs in a playlist
  • history of videos watched on YouTube

Construct a List

All lists are assigned a name and enclosed in square brackets.

List below is assigned to the variable name math_grades and contains a collection of scores for math exams for a single student.

There are 4 items in the list below.

math_grades = [88, 91, 82, 84]
[88, 91, 82, 84]

Lists Can Hold Multiple Object Types

Lists can contain multiple types of objects such as:

  • numeric types:
    • floats (example: 3.14)
    • ints (example: 3)
  • sequence types:
    • strings (example: "pass")
    • lists (example: [1, 2, 3])
    • tuples (example: (8, "pass"))
  • and more!
various_grades = [82, "pass", ["great", "ok", "very good"]]
[82, 'pass', ['great', 'ok', 'very good']]

Lists Are Ordered Sequences of Elements

The order of items in a list matters. Let's take a look at math_grades.

[88, 91, 82, 84]

88 is the score on the first exam, 91 on the second exam and so forth.

Index Items in a List

Index a list to retrieve a single or multiple items in a list. An index number is always an integer value and each the first item in a list is index number 0

For the list math_grades, here's the index breakdown:

index value
0 88
1 91
2 82
3 84

Retrieve the item at index 0.


Retrieve the item at index 1.