Python Lists: Create, Access, Modify, and Loop Through Lists
Think about your favorite music playlist. It has a bunch of songs stored in a specific order. You can add new songs, remove old ones, rearrange them, or skip to song number five.
A Python list works exactly like that. It's an ordered collection of items that you can change whenever you want. Grocery lists, student names, high scores, pixel colors — lists are one of the most useful tools in all of Python.
In this tutorial, you'll learn how to create lists, access items by position, add and remove items, loop through them, slice them, and avoid the most common mistakes beginners make.
How to Create a List in Python
You create a list by putting items inside square brackets [], separated by commas. Each item can be any type — strings, numbers, booleans, or even other lists.
Each item in the list has a position called an index. The first item is at index 0, the second is at index 1, and so on. This "start at zero" rule is the same in almost every programming language.
You can also create an empty list and add items to it later. This is common when you don't know ahead of time what your list will contain.
How to Access List Items by Index in Python
To grab a single item from a list, put its index number inside square brackets after the list name. Remember, indexing starts at 0, not 1.
Python also supports negative indexing, which counts from the end of the list. -1 is the last item, -2 is the second-to-last, and so on.
Negative indexing is especially handy when you want the last item but don't know how long the list is. my_list[-1] always gives you the last one.
How to Modify Lists in Python
Lists are mutable, which means you can change them after creating them. You can swap out individual items, add new ones, or combine lists together.
Changing an Item by Index
To replace a specific item, assign a new value to its index. This overwrites the old value completely.
Adding Items with append(), insert(), and extend()
Python gives you three ways to add items to a list. Each one works a little differently.
a = [1, 2, 3]
a.append([4, 5])
print(a) # [1, 2, 3, [4, 5]]a = [1, 2, 3]
a.extend([4, 5])
print(a) # [1, 2, 3, 4, 5]How to Remove Items from a Python List
Just like there are several ways to add items, Python gives you several ways to remove them. The right choice depends on whether you know the value or the position.
remove() searches for a value and deletes the first one it finds. If the value doesn't exist, Python raises a ValueError.
pop() removes by position and gives back the removed item. This is useful when you need to use that item after removing it — like dealing cards from a deck.
How to Loop Through a Python List
One of the most powerful things you can do with a list is loop through it — run some code once for every item. This is how you process a hundred items without writing a hundred lines.
Read this as: "for each fruit in the fruits list, print that fruit." Python picks up the first item, runs the indented code, picks up the next item, runs it again, and so on until the list is done.
Sometimes you need the index number along with the item. The enumerate() function gives you both.
You can also loop using a range of index numbers. This is useful when you need to compare or modify items by their position.
How to Check If an Item Is in a Python List
The in keyword checks whether a value exists in a list. It returns True or False.
This is much cleaner than writing a loop to search for a value manually. Use in anytime you need to check whether something exists in a list.
Python List Slicing
Slicing lets you grab a portion of a list instead of a single item. The syntax is list[start:end], where start is included and end is not included.
The "end is not included" rule feels weird at first, but it has a nice benefit: letters[:3] gives you exactly 3 items, and letters[3:] gives you the rest. No off-by-one confusion.
You can add a third number called the step. This tells Python how many items to skip between each pick.
Useful Python List Methods
Python lists come with many built-in methods. Here are the ones you'll use most often.
Common Python List Mistakes
Lists have a few tricky behaviors that trip up almost every beginner. Understanding these now will save you hours of debugging later.
a = [1, 2, 3]
b = a # b IS a
b.append(4)
print(a) # [1, 2, 3, 4]a = [1, 2, 3]
b = a[:] # b is a COPY of a
b.append(4)
print(a) # [1, 2, 3]Practice Exercises
Time to practice! These exercises build on each other, going from basic to more challenging. Try each one before looking at hints.
Create a list called animals containing these three strings: "cat", "dog", "rabbit".
Then print the length of the list in this exact format:
3A list of cities is given. Use negative indexing to print the last city in the list.
Expected output:
TokyoYou have a shopping list. Do these steps in order:
1. Add "bread" to the end of the list
2. Remove "milk" from the list
3. Print the final list
Expected output:
['eggs', 'cheese', 'butter', 'bread']This code tries to print the last color in the list, but it crashes with an IndexError. Fix the bug so it prints:
purpleHint: The list has 4 items. What are the valid indexes?
Write a program that calculates the sum of all numbers in the list using a for loop. Do not use the built-in sum() function.
Expected output:
150Find the largest number in the list using a for loop. Do not use the built-in max() function.
Expected output:
95Read this code carefully and predict what it will print. Think about what happens when two variables point to the same list.
Type the exact output (each value on a new line if there are multiple prints).
Reverse the given list using a loop. Do not use reverse(), reversed(), or [::-1].
Expected output:
[5, 4, 3, 2, 1]Summary: What You Learned About Python Lists
| Great job making it through this tutorial! Here's a recap of everything you now know: | ||
|---|---|---|
| --- | --- | --- |
| Create a list | Store items in order | fruits = ["a", "b"] |
| Access by index | Get one item | fruits[0], fruits[-1] |
| Modify items | Change a value | fruits[0] = "cherry" |
append() | Add to end | fruits.append("date") |
insert() | Add at position | fruits.insert(1, "fig") |
remove() | Delete by value | fruits.remove("a") |
pop() | Delete by index | fruits.pop(0) |
for loop | Process each item | for f in fruits: |
in / not in | Check membership | "a" in fruits |
| Slicing | Get a portion | fruits[1:3] |
sort() / reverse() | Reorder items | fruits.sort() |
len() / sum() | Count or total | len(fruits) |
Lists are one of the most frequently used data structures in Python. Almost every real program uses them — from web apps to data science to game development.
What's Next?
In the next tutorial — [Python List Comprehensions](/python/python-comprehensions) — you'll learn a powerful shortcut for creating and transforming lists in a single line of code.
You'll see how to replace 4 lines of loop code with one clean line, and learn when comprehensions make sense versus when a regular loop is better.