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.
[]
.\[ \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.
\[ \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.
\[ \begin{array}{lccccc} \texttt{course_info} & = & \bigg[ & \text{"DS105A"}, & 103, & 70.0, & True & \bigg] \\ & & & & & & \\ \text{Index:} & & & 0 & 1 & 2 & 3 \\ \end{array} \]
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:
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} \]
To add an element to the end of a list, you can use the append()
method.
For example, this line of code:
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} \]
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:
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} \]
{}
.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} \]
To access an element in a dictionary, you can use the key of the element.
For example:
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:
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} \]
Just like lists, dictionaries accept any data type as values. This means you can have dictionaries within dictionaries.
This is a common pattern when working with real-life (JSON) data.
For example:
To access the name of the lead instructor in the dictionary above, you would use the following code:
This would return a string with the value:
In this case, we chained the keys, one after the other, to navigate the dictionary.
LSE DS105A (2024/25) – Python Guides