Skip to main content
HomeAbout PythonLearn Python

Python Lists Tutorial

A list is a compound data type where you can group values together. A Python list is a convenient way of storing information altogether rather than individually.
Jun 2020  · 4 min read

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.

Python Courses

Introduction to Python

BeginnerSkill Level
4 hr
5.1M
Master the basics of data analysis with Python in just four hours. This online course will introduce the Python interface and explore popular packages.
See DetailsRight Arrow
Start Course
See MoreRight Arrow
Related

Free Access Week | Aug 28 – Sept 3

Access DataCamp's entire platform for free, including all 440+ courses, for an entire week. No catch, no credit card required—just unlimited learning for anyone with internet access.
Will Rix's photo

Will Rix

5 min

How to Choose The Right Data Science Bootcamp in 2023 (With Examples)

Learn everything about data science bootcamps, including a list of top programs to kickstart your career.
Abid Ali Awan's photo

Abid Ali Awan

10 min

DataCamp Portfolio Challenge: Win $500 Publishing Your Best Work

Win up to $500 by building a free data portfolio with DataCamp Portfolio.
DataCamp Team's photo

DataCamp Team

5 min

10 Essential Python Skills All Data Scientists Should Master

All data scientists need expertise in Python, but which skills are the most important for them to master? Find out the ten most vital Python skills in the latest rundown.

Thaylise Nakamoto

9 min

A Complete Guide to Socket Programming in Python

Learn the fundamentals of socket programming in Python
Serhii Orlivskyi's photo

Serhii Orlivskyi

41 min

Chroma DB Tutorial: A Step-By-Step Guide

With Chroma DB, you can easily manage text documents, convert text to embeddings, and do similarity searches.
Abid Ali Awan's photo

Abid Ali Awan

10 min

See MoreSee More