Lists and Arrays
Learn what lists and arrays are: how computers store many values in one place, how to add, read, and change items by their index, with clear examples and a quiz.
Key takeaways
- A list (or array) holds many values in one named place
- Each item has a position called an index, starting at 0
- You can read, change, add, and remove items in a list
- len() tells you how many items a list has
One box, many values
Imagine you want to store the names of everyone on a sports team. You could make a separate variable for each player:
player1 = "Sam"
player2 = "Alex"
player3 = "Jordan"
That works for three players, but it gets messy fast. What about a team of twenty? This is the problem lists and arrays solve. A list is a single named place that holds many values, kept in order.
team = ["Sam", "Alex", "Jordan"]
Now all three names live in one variable called team. In Python this is called a list. In many other languages, such as JavaScript, the same idea is called an array. The name is different but the idea is the same: a row of values stored in order.
Finding items with an index
Each item in a list has a position number called an index. Here is the surprising part: counting starts at 0, not 1.
team = ["Sam", "Alex", "Jordan"]
print(team[0]) # Sam
print(team[1]) # Alex
print(team[2]) # Jordan
So team[0] is the first item. Think of the index as "how many steps from the start." The first item is 0 steps away, the second is 1 step away, and so on.
If you ask for an index that doesn't exist, like team[5], the program stops with an error. So it helps to know how many items you have:
print(len(team)) # 3
len() is a function that returns the length — the number of items.
Changing an item
You can replace any item by assigning a new value to its index:
team = ["Sam", "Alex", "Jordan"]
team[1] = "Riley"
print(team) # ['Sam', 'Riley', 'Jordan']
Index 1 held "Alex", and now it holds "Riley". The rest of the list stays exactly the same.
Adding and removing items
Lists can grow and shrink while your program runs. To add an item to the end, use append():
team = ["Sam", "Alex"]
team.append("Jordan")
print(team) # ['Sam', 'Alex', 'Jordan']
To remove an item, use remove() with the value you want gone:
team.remove("Alex")
print(team) # ['Sam', 'Jordan']
Lists can hold numbers too
A list is not just for words. It can hold numbers, and you can do math with them:
scores = [8, 5, 10, 7]
print(scores[0] + scores[2]) # 8 + 10 = 18
You can even build a list up from nothing, starting empty:
scores = []
scores.append(8)
scores.append(5)
print(scores) # [8, 5]
Why this matters
Almost every real program uses lists or arrays. A music app keeps your songs in a list. A game keeps the enemies on screen in a list. A messaging app keeps your chats in a list. Once you can store many things in one place, you can write programs that handle real amounts of data.
The natural next step is to visit every item in a list automatically. That is exactly what loops are for — see Loops: Making the Computer Repeat and then Python Loops and Lists to combine the two ideas.
Practice challenges
- Make a list of your three favorite foods and print the middle one.
- Start with an empty list, append four numbers, then print how many it has with
len(). - Replace the first item in a list with a new value and print the result.
Quick quiz
Test yourself and earn XP
In the list `colors = ["red", "green", "blue"]`, what is at index 0?
Indexes start at 0, so colors[0] is the first item: red.
What does `len([10, 20, 30])` return?
len() counts the items. There are 3 items, so it returns 3.
How do you add 5 to the end of a list named `nums`?
append() adds a new item onto the end of the list.
After `pets = ["cat", "dog"]` and `pets[1] = "fish"`, what is the list?
Index 1 is the second item (dog), so it gets replaced by fish.
FAQ
They are very similar ideas: both store many values in order. Different languages use different names. Python calls them lists; JavaScript and many other languages call them arrays.
It comes from how computers measure distance from the start of the list. The first item is 0 steps from the beginning, the second is 1 step, and so on.
Keep exploring
More in Coding