
The Power of Lists
You will now look at the first type of data structure in Python: lists.
A list is a type of container in Python that is used to store multiple data sets at the same time. Python lists are often compared to arrays in other programming languages, but they do a lot more.

Figure 2.2: A Python list with a positive index
A list in Python is written within square brackets, [ ]. Each element in the list has its own distinct position and index. The elements in a list have a finite sequence. Like other programming languages, the index of the first item of a list is 0, and the second item has an index of 1, and so on. This has to do with how lists are implemented at a lower programming level, so do take note of this when you are writing index-based operations for lists and other iterable objects.
You will now look at the different ways that lists can be useful by completing Exercise 21, Working with Python Lists.
Exercise 21: Working with Python Lists
In this exercise, you will learn how to work with a Python list by coding and creating a list and adding items. For example, this could prove useful if, for instance, you have to use a list to store the items that are in a shopping cart:
- Open a new Jupyter Notebook.
- Now enter the following code snippet:
shopping = ["bread","milk", "eggs"]
print(shopping)
You should get the following output:
['bread', 'milk', 'eggs']
You created a list called shopping and added items to your list (bread, milk, and eggs).
Since a list is a type of iterable in Python, you can use a for loop to iterate over all of the elements inside a list.
- Now, enter and execute the code for a for loop and observe the output:
for item in shopping:
print(item)
You should get the following output:
bread
milk
eggs
Note
Python lists are different from arrays used in other languages, such as Java and C#. Python actually allows mixed types in a list, that is, int and string.
- Now use a mixed type of data within the list's content and enter the following code in a new cell:
mixed = [365, "days", True]
print(mixed)
You should get the following output:
[365, 'days', True]
But you might be wondering, in that case, shouldn't we be allowed to store a list of lists inside a list? Let's look at that in the next section. This is also known as a nested list, which can be used to represent a complex data structure.
In Exercise 21, Working with Python Lists, you were introduced to the basics of Python lists.
Later in this chapter, we will dive in deep and learn about the other types of lists that Python provides.
Matrices as Nested Lists
Most of the data we store in the real world is in the form of a tabular data table, that is, rows and columns, instead of a one-dimensional flat list. Such tables are called matrices or two-dimensional arrays. Python (and most other programming languages) does not provide a table structure out of the box. Programming languages do not provide table structures as it is not the role of a language to do so. A table structure is simply a way to present data.
What you can do is to present the table structure shown in Figure 2.3 using a list of lists; for example, if you want to store the following fruit orders using a list:

Figure 2.3: A representation of lists of lists as a matrix
Mathematically, you can present the information shown in figure 2.3 using a 2 x 3 (2 rows by 3 columns) matrix. This matrix would look like this:

Figure 2.4: A matrix representation of data
Now you will see how you can store this matrix as a nested list in the following exercise.
Exercise 22: Using a Nested List to Store Data from a Matrix
In this exercise, you will look at working with a nested list, storing values in it, and accessing it using a number of methods:
- Open a new Jupyter notebook.
- Enter the following code in a new cell:
m = [[1, 2, 3], [4, 5, 6]]
We can store the matrix as a series of lists inside a list which is called as a nested list.
- Now print list m:
print(m[1][1])
We can now access the elements using variable [row][column] notation.
You should get the following output:
5
It prints the value of row 2, column 2, which is 5 (remember, we are using a zero-based index offset).
- Now, access each of the elements in the nested list matrix by retaining their reference index with two variables, i and j:
for i in range(len(m)):
for j in range(len(m[i])):
print(m[i][j])
The preceding code uses a for loop to iterate twice. In the outer loop (i), we iterate every single row in matrix m, and in the inner loop (j), we iterate every column in the row. Finally, we print the element in the corresponding position.
You should get the following output:
1
2
3
4
5
6
- Use two for..in loops to print all the elements within the matrix:
for row in m:
for col in row:
print(col)
The for loop in the code used in step 4 iterates both the row and col. This type of notation does not require us to have prior knowledge of the matrix's dimensions.
You should get the following output:
1
2
3
4
5
6
At the end of this exercise, you know how a nested list stored as a matrix works. You have also got to know the different ways of accessing values from nested lists. In Activity 6, Using a Nested List to Store Employee Data, you will implement the concepts you have learned about lists and nested lists to store employee data.
Activity 6: Using a Nested List to Store Employee Data
You are going to store table data using a nested list. Imagine this: you are currently working in an IT company and are given the following list of employees. You are asked by your manager to use Python to store this data for further company use.
The aim of this activity is to use nested lists to store data and print them as you need them.
The data provided to you by your company is shown in Figure 2.5:

Figure 2.5: Table consisting of employee data
Follow these steps to complete this activity:
- Open a new Jupyter Notebook.
- Create a list and assign it to employees.
- Create three nested lists in employees to store the information of each employee, respectively.
- Print the employees variable.
- Print the details of all employees in a presentable format.
- Print only the details of Lisa Crawford.
By printing the details in a presentable format, you should get the following output:

Figure 2.6: Printed details of an employee using lists
Note
The solution for this activity is available on page 524.
In the next topic, you will be discussing a bit more about matrixes and their operations.