Tuples, sets and collection patterns
You have lists and dicts covered. Now let us look at two more collection types that round out your Python toolkit: tuples for immutable data and sets for deduplication.
Beyond lists and dicts, Python has two more collection types worth knowing. Tuples are like lists that cannot be changed after creation, which makes them useful for returning multiple values from a function.
# Tuples: like lists, but immutable (cannot be changed)
# Used for return values and fixed data
coordinates = (40.7128, -74.0060)
lat, lng = coordinates # unpacking assigns each value
print(f"Latitude: {lat}, Longitude: {lng}")Tuples use parentheses and cannot be modified after creation. Unpacking lets you assign each element to a separate variable in one line.
Sets are collections where every item is unique and duplicates are automatically removed, which is handy when you need to deduplicate results from multiple AI queries.
# Sets: collections of unique values (duplicates removed automatically)
categories = {"electronics", "clothing", "electronics", "food"}
print(categories) # {'electronics', 'clothing', 'food'}
# Useful for deduplicating results from AI queries
tags = ["python", "ai", "python", "ml", "ai"]
unique_tags = set(tags)
print(f"Unique tags: {unique_tags}") # {'python', 'ai', 'ml'}Sets automatically remove duplicates. Use set() to deduplicate lists, especially when combining results from multiple AI calls.
Matching exercise: Match the data structure
Loading practice…
Fill in the blanks: Build an API message
Loading practice…
Flashcards: Flashcards
Loading practice…
Validation checklist: Data structures checklist
Loading practice…