Learn Python: Lists and Tuples (Sequence Data Structures)
Python offers sequence data structures that allow you to store multiple items in a single object. Two of the most fundamental are lists and tuples. Lists are mutable, meaning you can add, remove, or modify elements after creation. Tuples are immutable, ideal for scenarios where the collection of elements should remain constant. This lesson shows you how to create, access, and manipulate these sequences.
What will be covered
• Defining lists with square brackets and performing common list operations.
• Accessing elements by index, including negative indices from the end.
• Slicing lists to extract sublists.
• Distinguishing lists from tuples, which use parentheses (or commas) and are immutable.
• Basic usage of len() to get lengths and the in operator to check membership.
Lists in Python
A list is defined by square brackets [] and can contain elements of any type, even mixing different types. You can add elements with methods like .append(), remove them with .remove() or .pop(), and insert new items at specific indices. Lists are dynamic: you can change them in place.
fruits = ["apple", "banana", "cherry"]
fruits.append("orange")
print(fruits)
fruits.remove("banana")
print(fruits)
fruits.insert(1, "blueberry")
print(fruits)
This script first adds “orange” to the end, removes “banana,” then inserts “blueberry” at index 1. Each time you print, you see how the list has changed.
Indexing and Slicing Lists
Access elements by their position, starting at zero. Negative indices count from the end (like -1 for last item). You can also slice a list using [start:end] to extract a sublist.
numbers = [10, 20, 30, 40, 50]
print(numbers[2]) # 30
print(numbers[-1]) # 50
print(numbers[1:4]) # [20, 30, 40]
print(numbers[:3]) # first three items [10, 20, 30]
print(numbers[3:]) # items from index 3 onward [40, 50]
Slicing numbers[1:4] returns a new list with indices 1, 2, and 3 (i.e., elements 20, 30, and 40). Negative slicing (like numbers[-3:-1]) also works for extracting a range near the end.
Tuples in Python
A tuple is defined by parentheses () or just commas. Once created, you cannot alter its size or change its elements. This immutability is useful when you want to ensure data remains unchanged.
date = (2025, 4, 7)
print(date)
print(date[0]) # 2025
# Attempting to modify
# date[0] = 2024 # would cause an error
The tuple date has three elements: year, month, and day. If you try to assign a new value to date[0], Python throws an error because tuples cannot be changed after creation.
When to Use Lists vs. Tuples
Lists are ideal when the collection needs to grow or shrink, or you plan to update items over time. Tuples are useful when the set of items is fixed, or you want to ensure the data remains constant. Tuples can also serve as quick structured records, like storing a (year, month, day) combination.
Practice Exercises
- Create a list of five fruits (strings) and print the list. Add a new fruit at the end, remove one fruit by name, and insert another fruit at index 2. Print after each operation.
- Access the third item in your fruit list (index 2) and print it. Then use a negative index to print the last item.
- Create a tuple representing a date, such as (2025, 4, 7). Print each element on a separate line. Attempt to modify one element and confirm Python raises an error (immutability).
- Slice the fruit list to get the first three fruits, then slice it again to get the last two fruits. Print each slice.
- Optional: check if a certain fruit (like “apple”) is in your fruit list using the in operator and print the result.
Summary
Lists and tuples provide flexible ways to store sequences of data. Lists are mutable, making them well-suited for dynamic collections where you add or remove items. Tuples, once created, cannot be changed, offering a form of read-only or fixed data structure. Both structures support indexing, slicing, and length checks, letting you easily access and manage elements. Knowing how to choose between them and perform basic operations is crucial for nearly all Python programming, from small scripts to extensive applications.