This guide introduces Python’s main data structures: Lists, Dictionaries, Sets, and Tuples. You’ll learn what they are, when to use them, and their strengths and weaknesses. Let’s make it easy to understand!
What Is a Data Structure?
A data structure is a way to organize and store data in a computer so you can use it efficiently. Think of it like organizing your closet: you arrange things to find, add, or change them easily. Data structures are key to writing good programs, and learning them is a big step in mastering Python.
In Python, data structures are either mutable (you can change them) or immutable (you can’t change them after creation). Python has four main built-in data structures:
-
Mutable: Lists, Dictionaries, Sets
-
Immutable: Tuples
There are also advanced data structures like stacks or queues, but they’re more for complex programming tasks, so we’ll skip them here. Let’s dive into the four main ones!
Lists
A list in Python is like a shopping list: it holds items in order, and you can add, remove, or change them. Lists are flexible because they can store different types of data (like numbers, text, or even other lists) and don’t need a fixed size.
Why Use Lists?
-
Great for storing a collection of related items.
-
Easy to modify (add, remove, or change items).
-
Useful for nested structures, like a list of lists for tables.
Downsides
-
Slow for math operations (use NumPy arrays for that).
-
Take up more memory than some other structures.
Examples
Here’s how to create and use lists:
# Create lists
empty_list = [] # Empty list
mixed_list = [1, 2, "3", 4] # Mixed data types
list_from_tuple = list((1, 2, 3)) # From a tuple
# Print lists
print(f"Empty list: {empty_list}")
print(f"Mixed list: {mixed_list}")
print(f"List from tuple: {list_from_tuple}")
Output:
Empty list: []
Mixed list: [1, 2, '3', 4]
List from tuple: [1, 2, 3]
You can access items using their position (index), starting at 0:
print(f"First item in mixed_list: {mixed_list[0]}") # Gets 1
print(f"Third item in list_from_tuple: {list_from_tuple[2]}") # Gets 3
You can also grab multiple items (slicing):
slice_list = mixed_list[2:] # Gets items from index 2 to end
print(slice_list) # ['3', 4]
Slicing uses [start:end], where start is included, but end is not. For example:
print(mixed_list[1:3]) # Gets items at index 1 and 2: [2, '3']
Lists are mutable, so you can change them:
empty_list.append(5) # Add 5
print(empty_list) # [5]
empty_list.remove(5) # Remove 5
print(empty_list) # []
mixed_list[2] = 5 # Change index 2 to 5
print(mixed_list) # [1, 2, 5, 4]
For more, check Python’s list documentation.
Dictionaries
A dictionary is like a phonebook: each key (like a name) is linked to a value (like a phone number). Keys must be unique, and dictionaries are great for quickly finding values using keys.
Why Use Dictionaries?
-
Clear and readable way to pair keys with values.
-
Super fast for looking up values by key.
Downsides
-
Use more memory than lists.
-
In Python 3.6+, dictionaries keep insertion order, but older versions don’t, which can cause issues.
Examples
Create dictionaries:
empty_dict = {} # Empty dictionary
people = {"John": {"Age": 27, "Hometown": "Boston"}, "Rebecca": {"Age": 31, "Hometown": "Chicago"}}
dict_from_list = dict([["one", 1], ["two", 2]]) # From a list of pairs
print(f"Empty dict: {empty_dict}")
print(f"People dict: {people}")
print(f"Dict from list: {dict_from_list}")
Output:
Empty dict: {}
People dict: {'John': {'Age': 27, 'Hometown': 'Boston'}, 'Rebecca': {'Age': 31, 'Hometown': 'Chicago'}}
Dict from list: {'one': 1, 'two': 2}
Access values:
print(people["John"]) # {'Age': 27, 'Hometown': 'Boston'}
Add or modify entries:
people["Violet"] = {"Age": 34, "Hometown": "Los Angeles"}
print(people)
Output:
{'John': {'Age': 27, 'Hometown': 'Boston'}, 'Rebecca': {'Age': 31, 'Hometown': 'Chicago'}, 'Violet': {'Age': 34, 'Hometown': 'Los Angeles'}}
Avoid duplicate keys, as only the last key-value pair is kept:
bad_dict = {"1": 1, "1": 2}
print(bad_dict) # {'1': 2}
Learn more in Python’s dictionary documentation.
Sets
A set is like a unique guest list: it holds only unique items, and you can’t have duplicates. Sets are great for removing duplicates or comparing collections (like finding common items).
Why Use Sets?
-
Perfect for unique operations like union or intersection.
-
Very fast for checking if an item exists.
Downsides
-
Unordered, so you can’t rely on item positions.
-
Can’t change items using indices like lists.
Examples
Create sets:
set1 = {1, 2, 3} # Using curly braces
set2 = set([1, 2, 3, 4]) # From a list
print(f"Set1: {set1}")
print(f"Set2: {set2}")
Output:
Set1: {1, 2, 3}
Set2: {1, 2, 3, 4}
Set elements must be immutable (like numbers or strings, not lists). Try this:
# This will fail
bad_set = { [1, 2], [3, 4] } # TypeError: unhashable type: 'list'
Set operations:
names1 = {"Glory", "Tony", "Joel", "Dennis"}
names2 = {"Morgan", "Joel", "Tony", "Emmanuel", "Diego"}
# Union: Combine sets (no duplicates)
union = names1 | names2
print(union) # {'Glory', 'Dennis', 'Diego', 'Joel', 'Emmanuel', 'Tony', 'Morgan'}
# Intersection: Common items
intersection = names1 & names2
print(intersection) # {'Joel', 'Tony'}
# Difference: Items in names1 but not names2
difference = names1 - names2
print(difference) # {'Dennis', 'Glory'}
Sets are much faster than lists for checking membership:
import time
s = set(range(10000000))
l = list(range(10000000))
start = time.time()
for i in range(5000):
i in s # Check if i is in set
print(f"Set check: {time.time() - start} seconds")
start = time.time()
for i in range(5000):
i in l # Check if i is in list
print(f"List check: {time.time() - start} seconds")
Output (times vary):
Set check: 0.0002 seconds
List check: 0.05 seconds
Check Python’s set documentation for more.
Tuples
A tuple is like a list, but you can’t change it after creation (it’s immutable). Think of it as a sealed box: once packed, it stays as is. Tuples are useful when you want data to stay fixed or use it as dictionary keys.
Why Use Tuples?
-
Immutable, so safe from accidental changes.
-
Can be dictionary keys (if all elements are immutable).
Downsides
-
Can’t modify them (use lists for that).
-
Use more memory than lists.
Examples
Create tuples:
tuple1 = (1, 2, 3, 4) # Using parentheses
tuple2 = tuple([1, 2, 3, 4, 5]) # From a list
print(f"Tuple1: {tuple1}")
print(f"Tuple2: {tuple2}")
Output:
Tuple1: (1, 2, 3, 4)
Tuple2: (1, 2, 3, 4, 5)
You can’t change tuples:
tuple1[0] = 10 # TypeError: 'tuple' object does not support item assignment
But you can access items like lists:
print(f"Second item in tuple2: {tuple2[1]}") # 2
Tuples as dictionary keys (must contain immutable elements):
hours = {("Rebecca", 1): 38, ("Thomas", 2): 40}
print(hours) # {('Rebecca', 1): 38, ('Thomas', 2): 40}
# This will fail
bad_hours = {(["Rebecca", 1]): 38} # TypeError: unhashable type: 'list'
Wrapping Up
Here’s what you’ve learned:
-
Data structures help store and manage data efficiently.
-
Python has mutable (Lists, Dictionaries, Sets) and immutable (Tuples) data structures.
-
Lists: Flexible for storing ordered, changeable collections.
-
Dictionaries: Fast for linking unique keys to values.
-
Sets: Great for unique items and comparisons (like unions).
-
Tuples: Immutable lists, safe for fixed data or dictionary keys.
Experiment with these structures to get comfortable, and check Python’s documentation for deeper dives!
Leave a Reply