Skip to main content

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

Beginner
4 hr
4.7M
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

Inside Our Favorite DataFramed Episodes of 2022

An inside look at our favorite episodes of the DataFramed podcast of 2022
Adel Nehme's photo

Adel Nehme

2 min

[Infographic] Data Science Project Checklist

Use this checklist when planning your next data science project.
Adel Nehme's photo

Adel Nehme

ChatGPT Cheat Sheet for Data Science

In this cheat sheet, gain access to 60+ ChatGPT prompts for data science tasks.
Travis Tang's photo

Travis Tang

10 min

An Introduction to Python Subprocess: Basics and Examples

Explore our step-by-step guide to running external commands using Python's subprocess module, complete with examples.
Moez Ali's photo

Moez Ali

15 min

Setting Up VSCode For Python: A Complete Guide

Experience a simple, fun, and productive way of Python development by learning about VSCode and its extensionsn and features.
Abid Ali Awan's photo

Abid Ali Awan

16 min

GeoPandas Tutorial: An Introduction to Geospatial Analysis

Get started with GeoPandas, one of the most popular Python libraries for geospatial analysis.
Javier Canales Luna's photo

Javier Canales Luna

15 min

See MoreSee More