Lists, Dicts & Sets

A visual guide

28 Oct 2024

In this guide, we will explore the various collection types available in Python, including lists, tuples, sets, and dictionaries. These collections are essential for managing and organizing data in Python.

Lists

  • Lists are ordered, mutable collections of items. They are defined using square brackets [].
# How to create a list from scratch
fruits = ["apple", "banana", "cherry"]
  • Lists are extremely flexible. The elements in a list do not need to be of the same data type.
  • Lists can contain any data type, including strings, integers, floats, and even other lists or other collections.

Visual Representation of a List

\[ \begin{array}{lccccc} \texttt{fruits} & = & \bigg[ & \text{"apple"}, & \text{"banana"}, & \text{"cherry"} & \bigg] \\ & & & \downarrow & \downarrow & \downarrow & \\ \text{Data Type:} & & & \text{str} & \text{str} & \text{str} & \\ \end{array} \]

In this case, all elements of the list are strings. After all, they are enclosed in double quotes.

Visual Representation of a List

\[ \begin{array}{lccccc} \texttt{course_info} & = & \bigg[ & \text{"DS105A"}, & 103, & 70.0, & True & \bigg] \\ & & & \downarrow & \downarrow & \downarrow & \downarrow \\ \text{Data Type:} & & & \text{str} & \text{int} & \text{float} & \text{bool} \\ \end{array} \]

Lists can take any data type. In this case, we have a mix of strings, integers, and floats in the same list.

Accessing Elements in a List

\[ \begin{array}{lccccc} \texttt{course_info} & = & \bigg[ & \text{"DS105A"}, & 103, & 70.0, & True & \bigg] \\ & & & & & & \\ \text{Index:} & & & 0 & 1 & 2 & 3 \\ \end{array} \]

To access an element in a list, you can use the index of the element. The index starts at 0.

We also use square brackets [] to access elements in a list.

For example:

course_info[0]  # "DS105A"
course_info[1]  # 103

Modifying Elements in a List

If I want to change the value of an element in a list, I can do so by assigning a new value to the element using its index.

For example, this line of code:

course_info[0] = "DS105A (2024/25)"

would make the list look like this:

\[ \begin{array}{lccccc} \texttt{course_info} & = & \bigg[ & \text{"DS105A (2024/25)"}, & 103, & 70.0, & True & \bigg] \\ \end{array} \]

Appending to the end of a List

To add an element to the end of a list, you can use the append() method.

For example, this line of code:

course_info.append("Python")

would make the list look like this:

\[ \begin{array}{lccccc} \texttt{course_info} & = & \bigg[ & \text{"DS105A (2024/25)"}, & 103, & 70.0, & True, & \text{"Python"} & \bigg] \\ \end{array} \]

Pop an Element out of a List

To remove an element from a list, you can use the pop() method, indicating the index of the element you want to remove.

For example, this line of code:

course_info.pop(3)

would make the list look like this:

\[ \begin{array}{lccccc} \texttt{course_info} & = & \bigg[ & \text{"DS105A (2024/25)"}, & 103, & 70.0, & \text{"Python"} & \bigg] \\ \end{array} \]

Dictionaries

  • Dictionaries also store collections of items, but they are unordered (there is no sense of index).
  • Instead of using indexes to access elements, dictionaries use keys.
  • Think of a dictionary as a list with named indexes instead of positional indexes.
  • Dictionaries are defined using curly braces {}.
# How to create a dictionary from scratch
course_info = {
    "course_code": "DS105A",
    "students": 103,
    "average_grade": 70.0,
    "is_active": True
}

Visual Representation of a Dictionary

When we create a dictionary, we define a key-value pair for each element.

We always lookup elements in a dictionary using the key.

\[ \begin{array}{llll} & & \text{KEY} & \text{VALUE} \\ \texttt{course_info = } & \\ & & \texttt{"course_code"} & \texttt{"DS105A"} \\ & & \texttt{"students"} & 103 \\ & & \texttt{"average_grade"} & 70.0 \\ & & \texttt{"is_active"} & \texttt{True} \\ \end{array} \]

Accessing Elements in a Dictionary

To access an element in a dictionary, you can use the key of the element.

For example:

course_info["students"]  # 103
course_info["is_active"]  # True

Modifying Elements in a Dictionary

If I want to change the value of an element in a dictionary, I can do so by assigning a new value to the element using its key.

For example, this line of code:

course_info["course_code"] = "DS105A (2024/25)"

would make the dictionary look like this:

\[ \begin{array}{llll} & & \text{KEY} & \text{VALUE} \\ \texttt{course_info = } & \\ & & \texttt{"course_code"} & \texttt{"DS105A (2024/25)"} \\ & & \texttt{"students"} & 103 \\ & & \texttt{"average_grade"} & 70.0 \\ & & \texttt{"is_active"} & \texttt{True} \\ \end{array} \]