Overview: Python's Core Data Structures
Python offers four primary built-in data structures, each with distinct characteristics and use cases. Understanding when to use each one is crucial for writing efficient, readable code.
| Feature | Dictionary | List | Set | Tuple |
|---|---|---|---|---|
| Ordered | Yes (3.7+) | Yes | No | Yes |
| Mutable | Yes | Yes | Yes | No |
| Duplicates | Keys: No | Yes | No | Yes |
| Access | By key | By index | Membership | By index |
| Syntax | {} | [] | {} or set() | () |
Dictionary vs List
Use Dictionary When:
- You need to look up values by a meaningful key
- Fast O(1) lookup is important
- You're storing key-value associations
- Order doesn't matter (or using Python 3.7+)
Use List When:
- You need ordered, sequential data
- You need to access elements by position
- Duplicates are allowed and meaningful
- You need to frequently add/remove from ends
# Dictionary: Fast lookup by key
users = {'alice': '[email protected]', 'bob': '[email protected]'}
email = users['alice'] # O(1) lookup
# List: Ordered sequence
emails = ['[email protected]', '[email protected]']
first_email = emails[0] # O(1) by index
found = '[email protected]' in emails # O(n) search!Dictionary vs Set
Sets and dictionaries are closely related - both use hash tables internally. A set is essentially a dictionary with only keys and no values.
Use Dictionary When:
- You need to associate values with keys
- You're counting occurrences
- You're mapping one thing to another
- You need to store metadata with items
Use Set When:
- You only need to track membership
- You need to remove duplicates
- You need set operations (union, intersection)
- Order doesn't matter
# Dictionary: Track counts
word_counts = {'apple': 3, 'banana': 2}
# Set: Track unique items only
unique_words = {'apple', 'banana', 'apple'} # {'apple', 'banana'}
# Set operations
set1 = {1, 2, 3}
set2 = {2, 3, 4}
print(set1 & set2) # Intersection: {2, 3}
print(set1 | set2) # Union: {1, 2, 3, 4}Dictionary vs Tuple
Tuples and dictionaries serve different purposes. Tuples are immutable sequences, while dictionaries are mutable mappings. Interestingly, tuples can be used as dictionary keys!
Use Dictionary When:
- You need named fields
- You'll add/remove items
- You need fast key-based lookup
- Structure may change at runtime
Use Tuple When:
- Data should be immutable
- You need a hashable sequence (dict key)
- You're returning multiple values
- Order matters and is fixed
# Dictionary: Named fields, mutable
person = {'name': 'Alice', 'age': 30}
person['age'] = 31 # Can modify
# Tuple: Immutable sequence
point = (10, 20)
# point[0] = 15 # Error! Tuples are immutable
# Tuple as dictionary key (useful for coordinates)
grid = {}
grid[(0, 0)] = 'start'
grid[(10, 20)] = 'end'Performance Comparison
| Operation | Dict | List | Set | Tuple |
|---|---|---|---|---|
| Lookup by key/index | O(1) | O(1) | N/A | O(1) |
| Search (in) | O(1) | O(n) | O(1) | O(n) |
| Insert | O(1) | O(1)* / O(n) | O(1) | N/A |
| Delete | O(1) | O(n) | O(1) | N/A |
| Memory | Higher | Lower | Higher | Lowest |
* O(1) for append, O(n) for insert at arbitrary position
When to Use Each
Dictionary
Configuration settings, caching, counting, mapping relationships, JSON data, database records
List
Ordered collections, queues/stacks (with deque), sequences where position matters, when you need duplicates
Set
Removing duplicates, membership testing, finding unique items, set operations (union, intersection)
Tuple
Immutable records, dictionary keys, function return values, coordinates, database rows
Converting Between Types
# List to Dict (with enumerate or zip)
fruits = ['apple', 'banana', 'cherry']
fruit_dict = dict(enumerate(fruits)) # {0: 'apple', 1: 'banana', 2: 'cherry'}
# Two lists to Dict
keys = ['a', 'b', 'c']
values = [1, 2, 3]
combined = dict(zip(keys, values)) # {'a': 1, 'b': 2, 'c': 3}
# Dict to List
my_dict = {'a': 1, 'b': 2}
keys_list = list(my_dict.keys()) # ['a', 'b']
values_list = list(my_dict.values()) # [1, 2]
items_list = list(my_dict.items()) # [('a', 1), ('b', 2)]
# List to Set (remove duplicates)
numbers = [1, 2, 2, 3, 3, 3]
unique = set(numbers) # {1, 2, 3}
# Set to List
back_to_list = list(unique) # [1, 2, 3]
# Dict keys to Set
my_dict = {'a': 1, 'b': 2}
key_set = set(my_dict.keys()) # {'a', 'b'}On This Page
- Overview
- Dict vs List
- Dict vs Set
- Dict vs Tuple
- Performance Comparison
- When to Use Each
- Converting Between Types
Related Content
- Dictionary Performance
Time complexity and optimization techniques
- Dictionary Methods
Complete reference of Python dictionary methods
- Glossary
Dictionary terms and definitions