In the previous lesson, we learned that a variable is like a single box holding one piece of data. But what if you need to store a collection of related items, like a shopping list, a set of high scores, or a list of user emails? Creating a separate variable for every single item would make your code messy and inefficient.
This is where a List comes in. In Python, a list is an ordered, changeable collection of items wrapped inside square brackets [ ]. While other languages call this structure an "array," Python lists are much more powerful because they can hold different types of data simultaneously (strings, integers, and Booleans all mixed together) and can grow or shrink dynamically as your script runs.
To create a list, you define a variable name, use the assignment operator =, and place your values inside square brackets, separating each item with a comma.
# Creating a list of tech terms (Strings)
programming_languages = ["Python", "JavaScript", "C++", "Rust"]
# Creating a mixed data type list
user_profile = ["Alex", 28, 94.5, True]
# Printing the entire list container
print(programming_languages)
Every single item inside a Python list has an assigned position number called an index. In computer science, counting always starts at 0, not 1! This means the very first item in your list is at index 0, the second item is at index 1, and so forth.
Python also supports negative indexing, which lets you look at the list backwards. An index of -1 always grabs the very last item in the list, which is highly useful when you don't know exactly how long your list is.
tech_tools = ["VS Code", "Terminal", "Git", "Docker"]
# Accessing elements by their position
print(tech_tools[0]) # This outputs: VS Code
print(tech_tools[2]) # This outputs: Git
# Accessing using negative index tricks
print(tech_tools[-1]) # This outputs the last element: Docker
Lists are mutable, meaning you can freely alter them after they are built. Python gives us built-in methods like .append() to push items to the end of a list, and standard index targeting to overwrite individual elements.
crypto_portfolio = ["Bitcoin", "Ethereum"]
# 1. Overwriting an existing item
crypto_portfolio[1] = "Solana"
print(crypto_portfolio) # Outputs: ['Bitcoin', 'Solana']
# 2. Appending a brand new item to the back
crypto_portfolio.append("Cardano")
print(crypto_portfolio) # Outputs: ['Bitcoin', 'Solana', 'Cardano']
Working with collections can be tricky at first. Keep an eye out for these two critical errors that crash beginner web tools and apps daily:
[3] will crash your script because the only valid indexes are 0, 1, and 2.
.append() alter your list directly in the background. Beginners often mistakenly write my_list = my_list.append("item"). Doing this will actually wipe out your list data and turn the variable completely blank (None).
Let's put this into action! Open your local text editor and complete this practical exercise:
Create a list variable named my_hobbies filled with three different activities.
Use the .append() method to add a fourth hobby to the collection.
Finally, print out only the second hobby in your list using the correct bracket index notation.