Skip to main content
HomeTutorialsPython

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 25, 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

Course

Introduction to Python

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

tutorial

Python Data Structures Tutorial

Get introduced to Python data structures: learn more about data types and primitive as well as non-primitive data structures, such as strings, lists, stacks, etc.
Sejal Jaiswal's photo

Sejal Jaiswal

24 min

tutorial

Python List Functions & Methods Tutorial and Examples

Learn about Python List functions and methods. Follow code examples for list() and other Python functions and methods now!
Abid Ali Awan's photo

Abid Ali Awan

7 min

tutorial

Python Dictionary Tutorial

In this Python tutorial, you'll learn how to create a dictionary, load data in it, filter, get and sort the values, and perform other dictionary operations.
DataCamp Team's photo

DataCamp Team

14 min

tutorial

Python Tuples Tutorial

Learn about Python tuples: what they are, how to create them, when to use them, what operations you perform on them and various functions you should know.
Sejal Jaiswal's photo

Sejal Jaiswal

10 min

tutorial

Python Classes Tutorial

In Python, everything is an object. Numbers, strings, DataFrames, even functions are objects. In particular, everything you deal with in Python has a class, a blueprint associated with it under the hood.
DataCamp Team's photo

DataCamp Team

3 min

tutorial

Python List Comprehension Tutorial

Learn how to effectively use list comprehension in Python to create lists, to replace (nested) for loops and the map(), filter() and reduce() functions, ...!
Aditya Sharma's photo

Aditya Sharma

20 min

See MoreSee More