When to use which sequence in Python: A Quick Guide
This article is a complementary resource to the Learn Python Basics course.
This article is a complementary resource to the Learn Python Basics course.
Python offers various sequence types—lists, tuples, sets, and dictionaries—to help you manage data efficiently.
The choice of sequence depends on your specific use case.
Here's a quick guide to help you understand their features and when to use them.
A list is an ordered, mutable collection that allows duplicates.
Suppose you need to manage a to-do list where you can add, remove, or update tasks as needed.
Lists are perfect for this because they maintain the order of items and allow modifications. For example,
tasks = ["Buy groceries", "Clean the house", "Pay bills"]
tasks.append("Walk the dog") # Adding an item
tasks[1] = "Clean the kitchen" # Modifying an item
print(tasks)
# Output: ['Buy groceries', 'Clean the kitchen', 'Pay bills', 'Walk the dog']
Here, you can see how easy it is to modify tasks by adding or updating items in the list. Lists are ideal for collections that may change over time.
A tuple is an ordered, immutable collection that also allows duplicates.
Suppose you need to store fixed data, such as geographic coordinates or configuration settings, that should not be modified.
Tuples are perfect for this purpose because they prevent accidental changes.
coordinates = (10, 20) # A fixed pair of values
# coordinates[0] = 15 # This would raise an error
print(coordinates)
# Output: (10, 20)
Since tuples are immutable, trying to change their values results in an error. Tuples are great for representing constant data that should remain unchanged.
A set is an unordered, mutable collection that only allows unique items.
Suppose you need to store email addresses for a newsletter subscription, and you want to ensure there are no duplicates.
A set is perfect for this, as it will automatically eliminate any repeated values. For example,
email_addresses = {"john@example.com", "jane@example.com", "john@example.com", "doe@example.com"}
print(email_addresses)
# Output: {'john@example.com', 'jane@example.com', 'doe@example.com'}
Here, the duplicate
"john@example.com"
is automatically removed, and only unique email addresses are retained. Sets are perfect for situations where uniqueness is crucial, such as storing a list of email addresses, user IDs, or tags.
A dictionary is an unordered, mutable collection of key-value pairs.
Suppose you need to store student scores where each student's name is linked to their corresponding score.
A dictionary allows you to efficiently store and retrieve this type of data.
student_scores = {"Alice": 90, "Bob": 85}
student_scores["Charlie"] = 92 # Add a new student
print(student_scores)
# Output: {'Alice': 90, 'Bob': 85, 'Charlie': 92}
Here, each student's name serves as a key, and their score is the value. Dictionaries are ideal for mapping relationships, such as associating user details with their profiles or items with their prices.
Sequence | Mutability | Order | Duplicates | Best For |
---|---|---|---|---|
List | Mutable | Ordered | Yes | Collections that change over time (e.g., to-do lists, tasks) |
Tuple | Immutable | Ordered | Yes | Fixed collections that should not change (e.g., coordinates, configurations) |
Set | Mutable | Unordered | No | Storing unique items (e.g., email addresses, tags) |
Dict | Mutable | Unordered | No (keys only) | Key-value pairs (e.g., user profiles, settings, student marks) |
Suppose you have the marks of three students (Alice, John, and Meera) for five subjects. Which sequence should you use to store the marks, and how would you access them?
You should use a dictionary to store the students' marks, as it allows you to associate each student with their marks efficiently.