Lesson 7: Python Lists and Tuples
When you write code, you rarely work with just a single piece of data. Usually, you need to store groups of related information—like a list of user names, a sequence of high scores, or the coordinates of a map location.
Instead of creating a hundred different variables for a hundred different items, Python gives you powerful containers to hold multiple items in a single variable. The two most common containers are Lists and Tuples.
While they might seem very similar at first glance, they have one massive difference that determines when you should use them.
1. Python Lists (The Flexible Container)
A list is exactly what it sounds like: a collection of items in a specific order. The most important feature of a list is that it is mutable, which is a fancy programming word meaning it can be changed. You can add items, remove items, or alter existing items after the list is created.
The Syntax: Lists are created using square brackets [], with each item separated by a comma.
# Creating a list of strings
movies = ["The Matrix", "Inception", "Interstellar"]
# Creating a list of mixed data types (Python allows this!)
user_data = ["Alice", 28, True]
Accessing Items (Indexing): To grab an item from a list, you use its index number inside square brackets. However, you must remember the golden rule of counting in Python: Python always starts counting at 0.
shopping_list = ["Apples", "Milk", "Bread"]
print(shopping_list[0]) # Prints: Apples
print(shopping_list[2]) # Prints: Bread
Modifying a List: Because lists are mutable, they come with built-in methods to change their contents.
scores = [90, 85, 88]
# Changing an existing item
scores[1] = 95 # The list is now [90, 95, 88]
# Adding a new item to the end
scores.append(100) # The list is now [90, 95, 88, 100]
# Removing a specific item
scores.remove(90) # The list is now [95, 88, 100]
2. Python Tuples (The Locked Vault)
A tuple (pronounced “too-pull” or “tuh-pull”) is also an ordered collection of items. However, its defining feature is that it is immutable. Once a tuple is created, its contents cannot be changed, added to, or removed. It is locked in place.
The Syntax: Tuples are created using parentheses () instead of square brackets.
# Creating a tuple
coordinates = (35.6895, 139.6917)
Accessing Items: You access items in a tuple the exact same way you do in a list—using square brackets and the index number.
dimensions = (800, 600)
print(dimensions[0]) # Prints: 800
What happens if you try to change it? If you try to alter a tuple, Python will immediately throw an error and crash your program.
rgb_color = (255, 0, 0)
# This will cause a TypeError!
rgb_color[0] = 128
Why Use a Tuple Instead of a List?
Since lists can do everything tuples can do, plus they can be modified, why would you ever use a tuple?
- Safety: If you have data that should never change while your program is running (like the days of the week, or tax brackets), putting it in a tuple guarantees you won’t accidentally alter it later in your code.
- Performance: Because tuples are locked and simpler behind the scenes, Python can process them slightly faster than lists.
- Intent: Using a tuple tells other developers reading your code, “These values belong together and are not meant to be modified.”
Ready to continue?
Move on to the next lesson to keep learning.
Continue: Lesson 8: Python Dictionaries and Sets