Skip to main content

Python Lists: Create, Access, Modify, and Loop Through Lists

Beginner30 min8 exercises145 XP
0/8 exercises

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.

Creating lists with square brackets
Loading editor...

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.

Empty lists and the list() constructor
Loading editor...

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.

Accessing items with positive indexes
Loading editor...

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.

Accessing items with negative indexes
Loading editor...

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.

Checking list length to avoid IndexError
Loading editor...

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.

Replacing an item by index
Loading editor...

Adding Items with append(), insert(), and extend()

Python gives you three ways to add items to a list. Each one works a little differently.

Three ways to add items
Loading editor...
append() adds as one item
a = [1, 2, 3]
a.append([4, 5])
print(a)  # [1, 2, 3, [4, 5]]
extend() adds each element
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() and pop()
Loading editor...

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.

del and clear()
Loading editor...

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.

Basic for loop over a list
Loading editor...

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.

Using enumerate() to get index and value
Loading editor...

You can also loop using a range of index numbers. This is useful when you need to compare or modify items by their position.

Looping with range and index
Loading editor...

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.

Using in and not in
Loading editor...

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.

Basic list slicing
Loading editor...

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.

Slicing with a step
Loading editor...

Useful Python List Methods

Python lists come with many built-in methods. Here are the ones you'll use most often.

sort(), reverse(), and count()
Loading editor...
len(), min(), max(), and sum()
Loading editor...

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.

Aliasing: both names point to the same list
Loading editor...
Making a proper copy with [:]
Loading editor...
Alias (same object)
a = [1, 2, 3]
b = a         # b IS a
b.append(4)
print(a)      # [1, 2, 3, 4]
Copy (separate object)
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.

Exercise 1: Create a List and Print Its Length
Write Code

Create a list called animals containing these three strings: "cat", "dog", "rabbit".

Then print the length of the list in this exact format:

3
Loading editor...
Exercise 2: Access the Last Item with Negative Indexing
Write Code

A list of cities is given. Use negative indexing to print the last city in the list.

Expected output:

Tokyo
Loading editor...
Exercise 3: Shopping List — Add and Remove Items
Write Code

You 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']
Loading editor...
Exercise 4: Fix the Bug — IndexError
Fix the Bug

This code tries to print the last color in the list, but it crashes with an IndexError. Fix the bug so it prints:

purple

Hint: The list has 4 items. What are the valid indexes?

Loading editor...
Exercise 5: Sum All Numbers Using a Loop
Write Code

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:

150
Loading editor...
Exercise 6: Find the Largest Number Without max()
Write Code

Find the largest number in the list using a for loop. Do not use the built-in max() function.

Expected output:

95
Loading editor...
Exercise 7: Predict the Output — List Aliasing
Predict Output

Read 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).

Loading editor...
Exercise 8: Reverse a List Without reverse() or [::-1]
Write Code

Reverse the given list using a loop. Do not use reverse(), reversed(), or [::-1].

Expected output:

[5, 4, 3, 2, 1]
Loading editor...

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 listStore items in orderfruits = ["a", "b"]
Access by indexGet one itemfruits[0], fruits[-1]
Modify itemsChange a valuefruits[0] = "cherry"
append()Add to endfruits.append("date")
insert()Add at positionfruits.insert(1, "fig")
remove()Delete by valuefruits.remove("a")
pop()Delete by indexfruits.pop(0)
for loopProcess each itemfor f in fruits:
in / not inCheck membership"a" in fruits
SlicingGet a portionfruits[1:3]
sort() / reverse()Reorder itemsfruits.sort()
len() / sum()Count or totallen(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.