Creating Lists
Concepts covered: Lists
A list holds any number of values in one variable, in the order you wrote them. Where a plain variable stores 1 score, a list stores 3 scores or 300 of them and keeps track of which came first. Python writes a list as square brackets around values separated by commas.
scores = [88, 92, 75]
days = ["Mon", "Tue", "Wed", "Thu"]
prices = [4.99, 12.50, 0.75]
empty = []The brackets mark where the list starts and ends, and each comma separates 1 item from the next. scores holds 3 integers, days holds 4 strings, prices holds 3 floats, and empty holds nothing at all, which still counts as a list.
scores = [88, 92, 75]
print(scores)
print(len(scores))
print(type(scores) == list)Output:
[88, 92, 75] 3 True
print shows the whole list in its bracket form, so every item is visible at once. len counts the items and returns 3 for scores. type(scores) == list is True, the same way type(88) == int and type("Mon") == str are both True. A list is a type of its own, and the values inside it keep their own types.
Items keep their order
A list remembers the sequence you gave it. A for loop visits each item from first to last, the same way it walks through range, except the loop variable receives the item itself rather than a count. The list below prints its 3 cities in the order they were written, every time the program runs.
cities = ["Lagos", "Oslo", "Lima"]
for city in cities:
print(city)
visited = []
print(visited)
print(len(visited))Output:
Lagos Oslo Lima [] 0
The empty list prints as [] and has length 0. A loop over an empty list runs its body 0 times and moves on. Many programs begin with an empty list and add items as they go, which Section 2 covers in detail.
Any type can go inside
A list takes values of any type, and a single list can mix them. A row from a spreadsheet fits in 1 list: the passenger's name and seat number sit beside the bag weight and a flag for whether the ticket is paid.
ticket = ["Ada", 34, 12.5, True]
print(ticket)
print(len(ticket))
print(type(ticket) == list)Output:
['Ada', 34, 12.5, True] 4 True
Python prints strings inside a list with single quotes, even when you typed double quotes. Both spellings make the same string, and the quotes are part of the display rather than part of the value.
| List | Items | len() |
|---|---|---|
| [88, 92, 75] | 3 integers | 3 |
| ["Mon", "Tue"] | 2 strings | 2 |
| ["Ada", 34, True] | a string, an int and a bool | 3 |
| [4.99] | 1 float | 1 |
| [] | nothing | 0 |
A list of 1 item is still a list, written with brackets around the lone value. The brackets, not the number of items, make something a list.
votes = ["yes", "no", "yes", "yes"]
print(votes)
print(len(votes))Output:
['yes', 'no', 'yes', 'yes'] 4
A list keeps repeated values too. Each "yes" is its own item in its own place, so 4 votes give a length of 4 even though only 2 different words appear. Section 3 depends on this, when a grocery list holds the same item twice.
len gives you a number
len returns an ordinary integer, so you can store it, do arithmetic with it or test it in an if like any other number. A small hotel keeps its booked guests and its waiting list as 2 separate lists. The front desk wants the total of both and the gap between them, and it also wants to know how many of its 5 rooms are still free.
booked = ["Ada", "Lin", "Omar"]
waitlist = ["Zoe"]
print(len(booked) + len(waitlist))
print(len(booked) - len(waitlist))
free = 5 - len(booked)
print("rooms free:", free)Output:
4 2 rooms free: 2
An empty list adds 0 to any total, so a sum of lengths counts exactly the items present. If the waiting list were [], the first line would print 3 and the second would print 3 as well.
What does this program print?
items = ["a", "b"]
more = []
print(len(items) + len(more))- It prints 2
- It prints 0
- It prints 1
- It raises an error
About this lesson
This section is part of the Lists: Beginner lesson on DataDriven, the home of data engineering. Lessons cover SQL, Python, Spark, data modeling and pipeline architecture, and each section pairs its explanation with worked examples and practice you run as you go.