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.

Topics

Python Courses

Certification available

Course

Introduction to Python

4 hr
5.4M
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

A Data Science Roadmap for 2024

Do you want to start or grow in the field of data science? This data science roadmap helps you understand and get started in the data science landscape.
Mark Graus's photo

Mark Graus

10 min

Python NaN: 4 Ways to Check for Missing Values in Python

Explore 4 ways to detect NaN values in Python, using NumPy and Pandas. Learn key differences between NaN and None to clean and analyze data efficiently.
Adel Nehme's photo

Adel Nehme

5 min

Seaborn Heatmaps: A Guide to Data Visualization

Learn how to create eye-catching Seaborn heatmaps
Joleen Bothma's photo

Joleen Bothma

9 min

Test-Driven Development in Python: A Beginner's Guide

Dive into test-driven development (TDD) with our comprehensive Python tutorial. Learn how to write robust tests before coding with practical examples.
Amina Edmunds's photo

Amina Edmunds

7 min

Exponents in Python: A Comprehensive Guide for Beginners

Master exponents in Python using various methods, from built-in functions to powerful libraries like NumPy, and leverage them in real-world scenarios to gain a deeper understanding.
Satyam Tripathi's photo

Satyam Tripathi

9 min

Python Linked Lists: Tutorial With Examples

Learn everything you need to know about linked lists: when to use them, their types, and implementation in Python.
Natassha Selvaraj's photo

Natassha Selvaraj

9 min

See MoreSee More