Python is beloved for its simplicity and power, especially when it comes to handling data. One of its strengths lies in its built-in data structures, which are super versatile and efficient for various tasks, from simple data storage to complex data manipulation.
This article will walk you through the core built-in data structures in Python:
- Lists
- Tuples
- Sets
- Dictionaries
- Strings (as data structures)
Each section will cover creation, key features, common methods, typical use cases, and important performance considerations. Whether you’re a beginner or just brushing up your skills, this will be a useful guide to master Python’s essential data tools.
Lists #
What is a List? #
Lists are ordered, mutable sequences of elements. You can store almost anything in a list—numbers, strings, objects, even other lists!
Creation, Indexing, and Slicing #
Creating a list is as simple as wrapping comma-separated values in square brackets:
fruits = ['apple', 'banana', 'cherry']
numbers = [1, 2, 3, 4, 5]
mixed = [1, 'two', 3.0, [4, 5]]
Lists are indexed starting at zero. You can access elements by their position:
print(fruits[0]) # apple
print(numbers[-1]) # 5 (negative indexing starts from the end)
Slicing allows you to extract sublists:
print(numbers[1:4]) # [2, 3, 4]
print(fruits[:2]) # ['apple', 'banana']
print(fruits[::2]) # ['apple', 'cherry'] (every second element)
Common Methods #
Python lists come packed with handy methods:
append(item)
: Adds an item to the end.insert(index, item)
: Inserts item at a specified index.pop(index)
: Removes and returns the item at index (default is last).remove(item)
: Removes first occurrence of item.sort()
: Sorts the list in-place.reverse()
: Reverses the list in-place.
Example:
numbers = [3, 1, 4]
numbers.append(2) # [3, 1, 4, 2]
numbers.insert(1, 10) # [3, 10, 1, 4, 2]
numbers.pop() # removes and returns 2
numbers.remove(10) # removes 10
numbers.sort() # [1, 3, 4]
numbers.reverse() # [4, 3, 1]
List Comprehension #
One of Python’s coolest features: list comprehensions. They let you create or transform lists in a single, readable line.
Example:
squares = [x**2 for x in range(6)] # [0, 1, 4, 9, 16, 25]
evens = [x for x in range(10) if x % 2 == 0] # [0, 2, 4, 6, 8]
They are often faster and cleaner than equivalent loops.
Use Cases and Performance #
Lists are your go-to for ordered collections you might need to modify. Common uses:
- Storing sequences of items
- Implementing stacks (using append/pop)
- Queues (though
collections.deque
is better for this) - Dynamic arrays
Performance wise:
- Access by index is O(1)
- Appending is generally O(1) amortized
- Insertions/removals in the middle are O(n), since elements need shifting
So if you need frequent insertions/removals not at the end, consider other structures.
Tuples #
Immutability #
Tuples are like lists, but immutable — once created, their contents can’t be changed.
Example:
point = (10, 20)
# point[0] = 5 # This would raise an error
Because of immutability, tuples are hashable if their elements are hashable. This allows them to be used as keys in dictionaries or elements of sets.
Tuple Packing and Unpacking #
Python makes it easy to pack values into a tuple and unpack them back:
# Packing
coordinates = 10, 20, 30 # parentheses optional
# Unpacking
x, y, z = coordinates
print(x, y, z) # 10 20 30
This feature shines in functions returning multiple values, swapping variables, and more:
a, b = b, a # swap values without temp variable
Use Cases #
- Fixed collections of heterogeneous data (e.g.,
(name, age, gender)
) - Dictionary keys or set elements (when composite keys are needed)
- Function arguments and returns
- Data integrity when you don’t want the data to be modified accidentally
Tuples can be more memory efficient than lists and faster in iteration.
Sets #
Creating and Modifying Sets #
Sets are unordered collections of unique elements. They’re great when you want to remove duplicates or do mathematical set operations.
Create sets with curly braces or the set()
function:
my_set = {1, 2, 3}
empty_set = set() # {} creates an empty dictionary, so use set() for empty set
my_set.add(4)
my_set.remove(2)
Set Operations #
Sets support classic operations:
- Union (
|
or.union()
): Combines unique elements from both sets - Intersection (
&
or.intersection()
): Elements common to both sets - Difference (
-
or.difference()
): Elements in one set but not the other - Symmetric difference (
^
or.symmetric_difference()
): Elements in either set but not both
Example:
a = {1, 2, 3}
b = {3, 4, 5}
print(a | b) # {1, 2, 3, 4, 5}
print(a & b) # {3}
print(a - b) # {1, 2}
print(a ^ b) # {1, 2, 4, 5}
Use Cases #
- Removing duplicates from lists
- Membership tests (
x in my_set
) are O(1), much faster than lists - Mathematical set operations in algorithms
- Keeping track of unique items
Dictionaries #
Key-Value Pairs #
Dictionaries store key-value pairs, letting you map unique keys to values.
Example:
person = {'name': 'Alice', 'age': 30, 'city': 'New York'}
Keys can be almost any immutable type (strings, numbers, tuples).
Accessing, Adding, Removing Elements #
Access by key:
print(person['name']) # Alice
Add or update:
person['job'] = 'Engineer'
Remove:
del person['age']
age = person.pop('age', None) # safer, returns None if key not found
Dictionary Methods #
.keys()
,.values()
,.items()
for iteration.get(key, default)
to safely get a value without KeyError.update()
to merge dictionaries.clear()
to empty the dict
Example:
for key, value in person.items():
print(f"{key}: {value}")
Nested Dictionaries and Real-World Examples #
Dictionaries can contain other dictionaries, enabling complex data structures:
company = {
'departments': {
'HR': {'employees': 10, 'budget': 100000},
'IT': {'employees': 25, 'budget': 250000},
},
'name': 'TechCorp'
}
Real-world example: JSON data maps naturally to nested dictionaries.
Strings as Data Structures #
String Manipulation #
Strings are sequences of characters. You can access, slice, and manipulate them much like lists:
s = "hello world"
print(s[0]) # 'h'
print(s[0:5]) # 'hello'
print(s[::-1]) # 'dlrow olleh' (reversed)
Common String Methods #
.lower()
,.upper()
,.capitalize()
.strip()
,.lstrip()
,.rstrip()
for trimming spaces.split()
and.join()
for breaking and combining strings.replace()
,.find()
,.startswith()
,.endswith()
Example:
text = " Python is awesome! "
clean = text.strip().upper().replace('AWESOME', 'great')
print(clean) # "PYTHON IS GREAT!"
Immutability and Use Cases #
Strings are immutable—once created, they cannot be changed. Operations that modify strings always create new string objects.
Use cases:
- Text processing and formatting
- Keys in dictionaries
- Data parsing, especially when combined with regex
- Storage of structured data (like CSV lines)
Final Thoughts #
Python’s built-in data structures are foundational and incredibly powerful. Knowing when and how to use lists, tuples, sets, dictionaries, and strings efficiently can make your code clearer, faster, and more Pythonic.
Here’s a quick recap:
Structure | Mutable? | Ordered? | Unique Items? | Use Cases |
---|---|---|---|---|
List | Yes | Yes | No | Ordered collections, stacks, queues |
Tuple | No | Yes | No | Fixed collections, keys in dicts |
Set | Yes | No | Yes | Unique items, fast membership testing |
Dictionary | Yes | Yes (Python 3.7+) | Keys unique | Key-value mappings, JSON-like data |
String | No | Yes | No | Text handling, keys, parsing |
The more you practice, the more intuitive these will become. So go ahead, experiment with these structures, combine them, and see how they can simplify your coding life!
If you want, I can also whip up code snippets or real-world mini-projects using these structures. Just let me know!