Lists - Intro to the Data Structure & Common Operations

What are Lists

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]

We can further verify math_grades is a list by checking its type.


Change an element in a list (mutability)

Since lists are mutable, we can change elements in the existing list data structure.

We can change the last grade to be 100. Re-assign the item at index 3 to be `100.

math_grades[3] = 100
[88, 91, 82, 100]

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