Lists
Hold a bunch of related values in one variable.
Make a list
Create a list
Square brackets, values separated by commas.
names = ["Steve", "Becca", "Alex"]
print(names)['Steve', 'Becca', 'Alex']
Start with an empty list
Empty square brackets. Common when the list fills up later, like as people sign up.
racers = []
racers.append("Becca")
print(racers)['Becca']
Get items
Counting starts at 0, so the first item is at index 0.
Get one item
The index goes in square brackets.
names = ["Steve", "Becca", "Alex"]
print(names[0])
print(names[1])Steve Becca
Asking for an index that is not there
3 items means the indexes are 0, 1, and 2. There is no index 3.
names = ["Steve", "Becca", "Alex"]
print(names[3])IndexError: list index out of range
Get the last item
-1 is the last one, no matter how long the list is.
names = ["Steve", "Becca", "Alex"]
print(names[-1])Alex
Count back from the end
-1 is the last, -2 is second to last, and so on.
names = ["Steve", "Becca", "Alex"]
print(names[-2])
print(names[-3])Becca Steve
Count items
Count the items
len() gives how many are in the list.
names = ["Steve", "Becca", "Alex"]
print(len(names))3
The last index with len()
The length minus 1 is the last index. This way works in almost every language.
names = ["Steve", "Becca", "Alex"]
last_index = len(names) - 1
print(names[last_index])Alex
Add items
Add to the end
append() is the one you will use most.
names = ["Steve", "Becca", "Alex"]
names.append("Yara")
print(names)['Steve', 'Becca', 'Alex', 'Yara']
Add at a position
Everything from that spot on shifts over.
names = ["Steve", "Becca", "Alex"]
names.insert(2, "Yara")
print(names)['Steve', 'Becca', 'Yara', 'Alex']
Add just before the last item
insert(-1, ...) puts it in front of the last item.
ingredients = ["bread", "ham", "cheese", "butter"]
ingredients.insert(-1, "beans")
print(ingredients)['bread', 'ham', 'cheese', 'beans', 'butter']
Change items
Replace an item
Assign straight to the index.
names = ["Steve", "Becca", "Alex"]
names[1] = "Yara"
print(names)['Steve', 'Yara', 'Alex']
Replace the last item
-1 works here too.
times = [10.81, 10.55, 10.92]
times[-1] = 10.65
print(times)[10.81, 10.55, 10.65]
Changing a spot that does not exist
You can only change an item that is already there. To add one, use append() or insert().
names = ["Steve", "Becca", "Alex"]
names[3] = "Yara"IndexError: list assignment index out of range
Take things out
Remove the last item
pop() with nothing inside takes the last one.
names = ["Steve", "Becca", "Alex"]
names.pop()
print(names)['Steve', 'Becca']
Remove by position
Give pop() an index to pick which one.
names = ["Steve", "Becca", "Alex"]
names.pop(0)
print(names)['Becca', 'Alex']
Remove by value
remove() takes the value itself, not the index.
names = ["Steve", "Becca", "Alex"]
names.remove("Becca")
print(names)['Steve', 'Alex']
remove() takes only the first match
There are two "Drew"s. remove() reads left to right and stops at the first one.
employees = ["Jimmy", "Drew", "Sophie", "Drew"]
employees.remove("Drew")
print(employees)['Jimmy', 'Sophie', 'Drew']
Removing something that is not there
remove() crashes if the value is not in the list. Check with in first when you are not sure.
names = ["Steve", "Becca", "Alex"]
names.remove("Yara")ValueError: list.remove(x): x not in list
Go through every item
Loop over a list
name holds a different item each time around.
names = ["Steve", "Becca", "Alex"]
for name in names:
print(f"Hi, {name}!")Hi, Steve! Hi, Becca! Hi, Alex!
Check if something is in the list
in and not in give back True or False.
names = ["Steve", "Becca", "Alex"]
if "Becca" in names:
print("Becca is here.")
if "Yara" not in names:
print("Yara is not here.")Becca is here. Yara is not here.