Tutorials

As opposed to data types such as int, bool, float, str, a list is a compound data type where you can group values together. In Python, it would be inconvenient to create a new python variable for each data point you collected. What you can do instead is, store all this information in a Python list. You can build such a list with square brackets.

[a, b, c]

Building a List

Suppose you asked your two sisters and parents for their height, in meters. You can build the list as follows:

[1.73, 1.68, 1.71, 1.89]
[1.73, 1.68, 1.71, 1.89]

Of course, all this data can be referenced to with a variable. Simply, put the variable name and the equal sign in front, like here.

fam = [1.73, 1.68, 1.71, 1.89]
fam
[1.73, 1.68, 1.71, 1.89]

A list is a way to give a single name to a collection of values. These values or elements can have any data type; int, float, etc., and also more advanced Python types, even other lists.

Storing Variables in Lists

It is also possible for a list to contain different types within it as well. Suppose you wanted to add the names of your parents and sisters to the list so you know which heights belong to whom. You can throw in some strings without issue.

fam = ["liz", 1.73, "emma", 1.68, "mom", 1.71, "dad", 1.89]
fam
['liz', 1.73, 'emma', 1.68, 'mom', 1.71, 'dad', 1.89]

That's not all though, you can also create smaller sublists inside of a list.

List Inside of a List

In the below example, the small lists are wrapped inside of square brackets and separated by a comma inside a list fam2. When you print the list fam2 you see that we have a list of lists. The main list contains 4 sublists:

fam2 = [["liz", 1.73],
        ["emma", 1.68],
        ["mom", 1.71],
        ["dad", 1.89]]
fam2
[['liz', 1.73], ['emma', 1.68], ['mom', 1.71], ['dad', 1.89]]

You can also check the data type of lists. Here we will see what type both fam and fam2 are:

type(fam)
list
type(fam2)
list

These calls show that both fam and fam2 are in fact lists. Each data type has a specific functionality and behavior associated with it. This is true for Python lists. They host a bunch of tools to subset and adapt them.

Interactive Example of List Creation

In this example, say after measuring the heights of your family members, you decided to collect some information on the house you're living in. The areas of the different parts of your home can be stored as separate variables for now.

You will create a list named areas, that contains the area of the hallway (hall), kitchen (kit), living room (liv), bedroom (bed), and bathroom (bath) in this order. Use the predefined variables. Finally, you will Print areas with the print() function.

# Area variables (in square meters)
hall = 11.25
kit = 18.0
liv = 20.0
bed = 10.75
bath = 9.50

# Create list areas
areas = [hall, kit, liv, bed, bath]

# Print areas
print(areas)

When you run the above code, it produces the following result:

[11.25, 18.0, 20.0, 10.75, 9.5]

Try it for yourself.

To learn more about creating lists, please see this video from our course Introduction to Python.

This content is taken from DataCamp’s Introduction to Python course by Hugo Bowne-Anderson.