Working with lists¶
In this tutorial, you will learn how to create, modify, and iterate over lists in Python.
Time commitment: 15–20 minutes
Prerequisites:
- Python 3.12 or later installed on your machine
- Basic familiarity with Python syntax (variables, strings, numbers)
Learning objectives¶
By the end of this tutorial, you will be able to:
- Create lists and access items by index
- Add, remove, and modify items in a list
- Iterate over lists using
forloops - Use slicing to extract parts of a list
What is a list?¶
A list is an ordered, mutable collection of items. Lists are one of the most commonly used data structures in Python. You can store any type of item in a list — numbers, strings, or even other lists.
Lists are created using square brackets []:
fruits = ["apple", "banana", "cherry"]
print(fruits)
Accessing items by index¶
Each item in a list has a position called an index. Python uses zero-based indexing, which means the first item is at index 0:
fruits = ["apple", "banana", "cherry"]
print(fruits[0]) # First item
print(fruits[1]) # Second item
print(fruits[2]) # Third item
You can also use negative indices to count from the end of the list. The index -1 refers to the last item:
fruits = ["apple", "banana", "cherry"]
print(fruits[-1]) # Last item
print(fruits[-2]) # Second to last
Finding the length of a list¶
Use the built-in len() function to find out how many items are in a list:
fruits = ["apple", "banana", "cherry"]
print(len(fruits))
Adding items¶
Lists are mutable, which means you can change them after they are created. Use append() to add an item to the end of a list:
fruits = ["apple", "banana"]
fruits.append("cherry")
print(fruits)
Use insert() to add an item at a specific position:
fruits = ["apple", "cherry"]
fruits.insert(1, "banana") # Insert at index 1
print(fruits)
Removing items¶
Use remove() to remove a specific item by its value:
fruits = ["apple", "banana", "cherry"]
fruits.remove("banana")
print(fruits)
Use pop() to remove an item by its index (and get it back):
fruits = ["apple", "banana", "cherry"]
removed = fruits.pop(1) # Remove item at index 1
print(f"Removed: {removed}")
print(f"Remaining: {fruits}")
Iterating over a list¶
Use a for loop to go through each item in a list:
fruits = ["apple", "banana", "cherry"]
for fruit in fruits:
print(f"I like {fruit}!")
If you need the index as well as the value, use enumerate():
fruits = ["apple", "banana", "cherry"]
for index, fruit in enumerate(fruits):
print(f"{index}: {fruit}")
Slicing¶
Slicing lets you extract a portion of a list. The syntax is list[start:stop], where start is included and stop is excluded:
numbers = [0, 1, 2, 3, 4, 5]
print(numbers[1:4]) # Items at index 1, 2, 3
print(numbers[:3]) # First three items
print(numbers[3:]) # Everything from index 3 onwards
print(numbers[-2:]) # Last two items
Checking membership¶
Use the in keyword to check whether an item exists in a list:
fruits = ["apple", "banana", "cherry"]
print("banana" in fruits)
print("grape" in fruits)
Exercise: build a to-do list¶
Now it is your turn! Create a list of three tasks you need to do today. Then:
- Add a fourth task
- Remove the second task (you have already done it)
- Print the remaining tasks with their positions
# Write your code here
Solution¶
Here is one way to complete the exercise:
tasks = ["Buy groceries", "Reply to emails", "Go for a walk"]
# Add a fourth task
tasks.append("Read a chapter")
# Remove the second task
tasks.pop(1)
# Print remaining tasks with positions
for index, task in enumerate(tasks, start=1):
print(f"{index}. {task}")
Summary¶
In this tutorial, you learned how to:
- Create lists using square brackets
[] - Access items using positive and negative indices
- Add items with
append()andinsert() - Remove items with
remove()andpop() - Iterate over lists with
forloops andenumerate() - Extract portions of a list using slicing
- Check whether an item exists using
in
What is next¶
In upcoming tutorials, you will explore:
- Tuples — immutable sequences for data that should not change
- Dictionaries — key-value pairs for fast lookups
- Sets — collections of unique items
- Comprehensions — concise ways to build data structures