Python Introduction to Lists
Table of Contents + −
In the last lesson you learned Regular Expressions, which help you find patterns inside text. Now we move to something you will use in almost every program you ever write. Real programs rarely deal with just one value at a time. You have many users. You have many scores. You have many items in a cart. So how do you hold all those values together without making a mess? That is exactly what a list is for.
🤔 Why Lists?
Say you want to store three friends’ names. Without lists, you would do this.
friend1 = "Alex"friend2 = "Riya"friend3 = "Arjun"That already feels clumsy. Now imagine fifty friends. You are not going to type friend50. And if you wanted to count them or go through them one by one, you would be stuck.
A list fixes this. It lets you keep all those values together in one variable.
friends = ["Alex", "Riya", "Arjun"]One name, one box, all three values inside. That is the whole idea.
🧩 What Is a List?
A list is an ordered collection that holds many values in one variable. Let me explain that word “ordered”. It means the values stay in the position you put them. The first one stays first. The second stays second. They do not move around on their own.
Think of a list like a shopping list on paper. You write items down in a line, top to bottom. Each item has its place. You can read the third item. You can cross one out. You can add a new one at the bottom. A Python list works the same way.
You create a list with square brackets [ ]. You separate the values with commas.
scores = [90, 85, 72, 100]print(scores)This makes a list of four numbers and prints it.
Output
[90, 85, 72, 100]🎨 A List Can Hold Different Types
A list is not picky. It can hold numbers, text, True/False values, even other lists. They do not all have to be the same type.
Here is a single list that mixes a name, an age, and whether the person is active.
person = ["Alex", 28, True]print(person)Output
['Alex', 28, True]Most of the time you will keep one kind of thing in a list. All scores, or all names. But it is good to know that Python lets you mix when you need to.
🔢 Accessing Items by Index
Storing values is only half the job. You also need to pull one back out. For that, each item in a list has a position number called an index.
Here is an important point. Counting starts at zero, not one. So the first item is at index 0. The second is at index 1. And so on.
You grab an item by writing the list name followed by the index in square brackets.
fruits = ["apple", "banana", "cherry"]print(fruits[0])print(fruits[1])print(fruits[2])Read it line by line. fruits[0] asks for the item at position zero, which is the first one, "apple". fruits[1] is the second, "banana". fruits[2] is the third, "cherry".
Output
applebananacherryThis table shows how the positions line up.
| Item | ”apple" | "banana" | "cherry” |
|---|---|---|---|
| Index | 0 | 1 | 2 |
So with three items, the valid indexes are 0, 1, and 2. There is no index 3 here. If you ask for one that does not exist, Python stops and tells you.
fruits = ["apple", "banana", "cherry"]print(fruits[3])Output
Traceback (most recent call last): File "main.py", line 2, in <module> print(fruits[3])IndexError: list index out of rangeThat IndexError just means you reached past the end of the list.
⏪ Counting From the End With Negative Index
Sometimes you want the last item but you do not know how long the list is. You could count it, but Python gives you a shortcut. A negative index counts from the end backwards.
So -1 is the last item. -2 is the second to last. And so on.
fruits = ["apple", "banana", "cherry"]print(fruits[-1])print(fruits[-2])fruits[-1] gives the last item, "cherry". fruits[-2] gives the one before it, "banana".
Output
cherrybananaThis is handy. You do not care how big the list grows. The last item is always at -1.
✏️ Lists Can Be Changed (Mutable)
Here is an important point. A list is mutable, which is a fancy word that just means changeable. After you make a list, you can still change what is inside it. You do not have to throw it away and build a new one.
To change an item, point at its index and assign a new value with =.
Say Riya had a score of 72 but it got corrected to 88. You find her spot and update it.
scores = [90, 72, 100]scores[1] = 88print(scores)The line scores[1] = 88 reaches into position 1 and replaces 72 with 88. Everything else stays put.
Output
[90, 88, 100]Compare that to text. A string in Python cannot be changed letter by letter like this. A list can. That difference is a big reason lists are so useful.
🛒 A Real Example: A Shopping List
Let us put it all together with something you would actually do. You keep a shopping list. You read an item, you fix a mistake, and you check the last thing on it.
shopping = ["milk", "bread", "egs", "butter"]
print("First item:", shopping[0])print("Last item:", shopping[-1])
# Oops, "egs" is a typo. Fix the item at index 2.shopping[2] = "eggs"
print("Fixed list:", shopping)Walk through it. First we print the item at index 0, "milk". Then the item at -1, which is "butter". Then we spot a typo, "egs", sitting at index 2, and we correct it to "eggs". Finally we print the whole list to confirm the fix.
Output
First item: milkLast item: butterFixed list: ['milk', 'bread', 'eggs', 'butter']That is reading, fixing, and checking, all on one list. This is the everyday work you will do with lists.
⚠️ Common Mistakes
A few slip-ups confuse many people at first.
- Starting the count at 1. The first item is index
0, not1. Somylist[1]is the second item.
names = ["Alex", "Riya"]
# ❌ Avoid: thinking this gives the first nameprint(names[1]) # this is "Riya", the second one
# ✅ Good: index 0 is the first nameprint(names[0]) # "Alex"-
Asking for an index that does not exist. A list with three items has indexes 0, 1, 2. Reaching for index 3 raises an
IndexError. -
Forgetting the commas. Each value must be separated by a comma, or Python will not read your list the way you expect.
# ❌ Avoid: missing commasnums = [1 2 3]
# ✅ Good: commas between every valuenums = [1, 2, 3]✅ Best Practices
Small habits that keep your lists easy to work with.
- Name the list for what it holds, in plural.
scores,friends,prices. So when you readscores[0]later, it is obvious what is inside. - Keep one kind of thing in a list when you can. All scores, or all names. It makes the list easy to reason about, even though Python allows mixing.
- Reach for
-1when you want the last item. It is cleaner than counting the length yourself. - Use indexes carefully. If you are not sure how long a list is, that last item is safest grabbed with
-1.
🧩 What You’ve Learned
A quick recap of what you can now do.
- ✅ A list is an ordered collection that holds many values in one variable.
- ✅ You create a list with square brackets and commas:
[90, 85, 72]. - ✅ A list can hold different types, like text, numbers, and True/False together.
- ✅ You read an item by its index, and counting starts at
0. - ✅ A negative index counts from the end, so
-1is the last item. - ✅ Lists are mutable, meaning you can change an item after the list is made.
Check Your Knowledge
Test what you learned. Pick an answer for each question, then click Check.
- 1
What does fruits[0] return for fruits = ['apple', 'banana', 'cherry']?
Why: Index counting starts at 0, so fruits[0] is the first item, 'apple'.
- 2
Which index gives you the last item of any list, no matter its length?
Why: A negative index counts from the end, and -1 always points to the last item.
- 3
What does it mean that a list is mutable?
Why: Mutable means changeable, so you can update an item by assigning to its index.
- 4
What happens when you access scores[3] for scores = [90, 85, 72]?
Why: The valid indexes are 0, 1, and 2, so index 3 is past the end and raises an IndexError.
🚀 What’s Next?
Now that you can create a list and read items from it, the next step is doing more with it: adding, removing, sorting, and joining lists together.