Welcome to the ultimate guide on Python’s built-in data structures! In this chapter, we dive deep into Lists, Tuples, Dictionaries, and Sets. Whether you’re storing a collection of favorite pizza toppings or building an intricate data model, these structures are your best friends—if they were people, they’d definitely be on your VIP list.
Lists are ordered, mutable collections that can hold elements of any data type. They’re like the buffet of Python data structures: mix and match to your heart’s content.
[]
.Example:
# A simple list of integers
my_list = [1, 2, 3, 4]
# A mixed list
mixed_list = [42, "hello", 3.14, True, [1, 2, 3]]
Slicing: Retrieve a subset of items with my_list[start:stop:step]
.
# Access the third element (index 2)
item = my_list[2] # 3
# Get a sublist from index 1 to 3 (3 is not included)
sublist = my_list[1:3] # [2, 3]
# Reverse a list using slicing
reversed_list = my_list[::-1] # [4, 3, 2, 1]
append()
, insert()
, or extend()
.Removing Elements: Use remove()
, pop()
, or del
.
# Modify an element
my_list[3] = 5 # Now my_list becomes [1, 2, 3, 5]
# Append adds to the end
my_list.append(6) # [1, 2, 3, 5, 6]
# Insert at a specific index (inserting 10 at index 1)
my_list.insert(1, 10) # [1, 10, 2, 3, 5, 6]
# Extend with another list
my_list.extend([7, 8]) # [1, 10, 2, 3, 5, 6, 7, 8]
# Remove by value (first occurrence)
my_list.remove(10) # [1, 2, 3, 5, 6, 7, 8]
# Pop removes and returns the element at a specific index (default is the last)
last_item = my_list.pop() # Removes 8
# Delete using 'del' (removes element at index 0)
del my_list[0] # Now my_list is [2, 3, 5, 6, 7]
for
loops.Comprehensions: Create lists in one concise line—your shortcut to less code and more coffee time!
# Looping through a list
for number in my_list:
print(number)
# List comprehension: square each number
squared = [x ** 2 for x in my_list]
IndexError
.copy()
or the copy
module to avoid accidental modifications when duplicating nested lists.Tuples are like lists that took an oath of silence—they’re immutable. Once created, they remain constant, which makes them great for storing data that should not change.
()
or even omit them in some cases.Example:
# Defining a tuple
my_tuple = (1, 2, 3)
# Parentheses are optional when the context is unambiguous
another_tuple = 4, 5, 6
Indexing: Works just like lists.
item = my_tuple[1] # 2
No Modifications: Any attempt to modify a tuple will raise a TypeError
.
# This will raise an error:
# my_tuple[1] = 10
Dictionaries are your go-to when you need a quick lookup table. They store data in key-value pairs, offering blazing-fast access and a more descriptive way of organizing data.
{}
or the dict()
function.Example:
# Using curly braces
my_dict = {'name': 'John', 'age': 30, 'city': 'New York'}
# Using the dict() function with a list of tuples
my_dict = dict([('name', 'John'), ('age', 30), ('city', 'New York')])
Access by Key:
name = my_dict['name'] # 'John'
Safe Access with get()
:
country = my_dict.get('country', 'United States') # Returns default if key doesn't exist
Modifying Values and Adding Pairs:
# Update a value
my_dict['age'] = 31
# Add a new key-value pair
my_dict['occupation'] = 'Engineer'
Deleting Pairs:
# Using del
del my_dict['city']
# Using pop (also returns the value)
age = my_dict.pop('age')
Iterate Over Keys:
for key in my_dict:
print(key, my_dict[key])
Iterate Over Items:
for key, value in my_dict.items():
print(f"{key}: {value}")
update()
or the {**dict1, **dict2}
syntax in Python 3.5+.Dictionary Comprehensions: Create dictionaries on the fly.
# Create a dictionary with numbers and their squares
squares = {x: x**2 for x in range(5)} # {0: 0, 1: 1, 2: 4, 3: 9, 4: 16}
Sets are unordered collections of unique elements. They’re perfect for membership testing, deduplication, and mathematical operations like unions and intersections.
{}
or the set()
constructor.Example:
# Define a set
my_set = {1, 2, 3}
# An empty set must be created with set(), not {} (which creates an empty dict)
empty_set = set()
Adding and Removing:
# Add an item
my_set.add(4)
# Remove an item (raises KeyError if not found)
my_set.remove(2)
# Discard an item (won't raise an error if item not present)
my_set.discard(10)
Mathematical Set Operations:
set_a = {1, 2, 3}
set_b = {3, 4, 5}
# Union: All unique items from both sets
union = set_a | set_b # {1, 2, 3, 4, 5}
# Intersection: Items common to both sets
intersection = set_a & set_b # {3}
# Difference: Items in set_a but not in set_b
difference = set_a - set_b # {1, 2}
# Symmetric difference: Items in either set, but not in both
sym_diff = set_a ^ set_b # {1, 2, 4, 5}
Efficiency: Sets are optimized for fast membership testing—like having an exclusive guest list for your data party.
if 3 in my_set:
print("3 is in the set")
Data Structure | Mutability | Ordered? | Use Case Example |
---|---|---|---|
List | Yes | Yes | Dynamic arrays, ordered collections |
Tuple | No | Yes | Fixed collections, keys in dictionaries |
Dictionary | Yes | Insertion-ordered (3.7+) | Fast lookups by unique keys, configuration data |
Set | Yes | No | Membership testing, deduplication, set math |
get()
method for dictionaries to avoid KeyError
.discard()
for sets if you’re not sure whether an element exists.Nested Data Structures:
Combine these structures to represent complex data. For instance, a list of dictionaries can represent a table of data:
students = [
{'name': 'Alice', 'age': 24, 'major': 'Physics'},
{'name': 'Bob', 'age': 22, 'major': 'Mathematics'},
{'name': 'Charlie', 'age': 23, 'major': 'Computer Science'}
]
Immutability for Safety:
When passing data to functions, using tuples can prevent accidental modifications, adding an extra layer of security to your code.
Comprehensions Everywhere:
List, dictionary, and set comprehensions offer a compact syntax to create new collections. They not only reduce code lines but also enhance readability once you get the hang of them.
Debugging Collections:
Use built-in functions like len()
, type()
, and even print statements to inspect your data structures. Tools like Python’s debugger (pdb) can be lifesavers when your data isn’t behaving as expected.