Python – Data Structures in Depth
Python provides four essential built-in collection types: list, tuple, set and dictionary. Lists and tuples maintain order, sets store unique elements, and dictionaries store key-value pairs. Understanding their behaviour, mutability, lookup performance and suitable use cases helps you write cleaner and more efficient Python applications.
Almost every real-world Python application works with collections of data. An e-commerce application stores products, a banking system processes transactions, and an employee management system maintains employee records. Choosing the right data structure makes these operations easier to implement and more efficient to execute.
In this article, we will explore Python's built-in data structures in depth, understand how they behave, compare their performance, and build practical examples using Python 3.
1. What Are Data Structures?
A data structure is a way of organising and storing data so that a program can access, update, search, insert or delete information efficiently.
Imagine an application that needs to store 500 employee records. We could create hundreds of separate variables, but that would be difficult to maintain. A collection allows us to manage related values together.
employee_1 = "Ananya"
employee_2 = "Rahul"
employee_3 = "Kiran"
# A list is easier to manage
employees = ["Ananya", "Rahul", "Kiran"]
print(employees)
['Ananya', 'Rahul', 'Kiran']
Python provides several built-in data structures. The four most commonly used collection types are:
2. Python Lists – Ordered and Mutable Collections
A list is an ordered collection that can store multiple values. Lists are mutable, which means we can add, remove or modify elements after creating them.
Lists can contain duplicate values and can store different data types, although keeping related values of a consistent type often makes application code easier to understand.
products = ["Laptop", "Keyboard", "Mouse"]
print(products)
print(products[0])
print(products[-1])
print(len(products))
['Laptop', 'Keyboard', 'Mouse'] Laptop Mouse 3
2.1 Accessing List Elements
Python uses zero-based indexing. The first element is at index 0, the second at index 1, and so on. Negative indexing accesses elements from the end.
numbers = [10, 20, 30, 40, 50]
print(numbers[0]) # First element
print(numbers[-1]) # Last element
print(numbers[1:4]) # Index 1 up to, but not including, 4
print(numbers[:3]) # First three elements
print(numbers[2:]) # From index 2 to the end
print(numbers[::-1]) # Reversed copy
10 50 [20, 30, 40] [10, 20, 30] [30, 40, 50] [50, 40, 30, 20, 10]
2.2 Common List Methods
| Method | Purpose | Example |
|---|---|---|
append() |
Adds one element at the end | items.append("Pen") |
extend() |
Adds elements from an iterable | items.extend(["Book", "Bag"]) |
insert() |
Inserts at a specified index | items.insert(1, "Pencil") |
remove() |
Removes the first matching value | items.remove("Pen") |
pop() |
Removes and returns an element | items.pop() |
sort() |
Sorts the list in place | items.sort() |
reverse() |
Reverses the list in place | items.reverse() |
index() |
Returns the first matching index | items.index("Book") |
count() |
Counts matching elements | items.count("Book") |
clear() |
Removes all elements | items.clear() |
2.3 Modifying a List
cart = ["Laptop", "Mouse"]
# Add one item
cart.append("Keyboard")
# Add multiple items
cart.extend(["Monitor", "Webcam"])
# Update an existing item
cart[1] = "Wireless Mouse"
# Remove an item by value
cart.remove("Webcam")
# Remove and return the last item
removed_item = cart.pop()
print(cart)
print("Removed:", removed_item)
['Laptop', 'Wireless Mouse', 'Keyboard', 'Monitor'] Removed: Monitor
append() adds its argument as a single element. extend() iterates over the supplied iterable and adds its elements individually.
first = [1, 2]
first.append([3, 4])
print(first)
second = [1, 2]
second.extend([3, 4])
print(second)
[1, 2, [3, 4]] [1, 2, 3, 4]
3. Python Tuples – Ordered and Immutable Collections
A tuple is an ordered collection that cannot be modified after it is created. Once a tuple is constructed, you cannot replace, add or remove its elements.
Tuples are useful when a group of values should remain fixed, such as geographic coordinates, RGB colour values, or a function returning multiple related results.
coordinates = (15.1394, 76.9214)
rgb_color = (255, 120, 50)
print(coordinates[0])
print(coordinates[1])
print(rgb_color)
15.1394 76.9214 (255, 120, 50)
3.1 The Single-Element Tuple
A common mistake is forgetting the comma when creating a tuple with only one element.
not_a_tuple = (10)
single_item_tuple = (10,)
print(type(not_a_tuple))
print(type(single_item_tuple))
<class 'int'> <class 'tuple'>
3.2 Tuple Packing and Unpacking
Python allows multiple values to be packed into a tuple and unpacked into separate variables.
employee = ("E102", "Ananya", "Engineering")
employee_id, name, department = employee
print(employee_id)
print(name)
print(department)
E102 Ananya Engineering
3.3 When Should You Use a Tuple?
- When the collection represents a fixed group of values.
- When you want to prevent accidental element reassignment.
- When returning multiple values from a function.
- When you need an immutable sequence that can be used as a dictionary key, provided all its elements are hashable.
record = ("Ananya", ["Python", "SQL"])
# The tuple element cannot be reassigned
# record[0] = "Rahul" # TypeError
# But the nested list can be modified
record[1].append("AWS")
print(record)
('Ananya', ['Python', 'SQL', 'AWS'])
4. Python Sets – Collections of Unique Elements
A set is a collection of unique, hashable elements. Duplicate values are automatically removed. Sets are useful for removing duplicates, checking membership and performing mathematical set operations.
Sets do not support indexing or slicing. Although Python sets preserve no positional contract, their iteration order may appear consistent in a particular execution. Do not depend on that order.
departments = {"HR", "IT", "Finance", "IT", "HR"}
print(departments)
print(len(departments))
The set contains the unique values:
{'HR', 'IT', 'Finance'} # Display order may vary
3
4.1 Adding and Removing Set Elements
skills = {"Python", "SQL"}
skills.add("AWS")
skills.update(["Docker", "Git"])
skills.discard("Java")
skills.remove("SQL")
print(skills)
A set containing: Python, AWS, Docker and Git The iteration order may vary.
Two commonly used removal methods are discard() and remove().
remove(value)raisesKeyErrorif the value is not present.discard(value)does nothing if the value is not present.
4.2 Mathematical Set Operations
Sets support operations commonly used in data analysis, access management and comparing collections.
python_users = {"Ananya", "Rahul", "Kiran"}
sql_users = {"Rahul", "Kiran", "Meera"}
# Members in either set
print(python_users | sql_users)
# Members common to both sets
print(python_users & sql_users)
# Members in Python users but not SQL users
print(python_users - sql_users)
# Members in exactly one of the sets
print(python_users ^ sql_users)
Union: Ananya, Rahul, Kiran, Meera Intersection: Rahul, Kiran Difference: Ananya Symmetric difference: Ananya, Meera Set display order may vary.
4.3 Removing Duplicates from a List
One common use case for sets is removing duplicate values from a list.
employee_ids = [101, 102, 101, 103, 102, 104]
unique_ids = set(employee_ids)
print(unique_ids)
{101, 102, 103, 104}
Set display order may vary.
If the original insertion order must be preserved, use an order-preserving approach:
employee_ids = [101, 102, 101, 103, 102, 104]
unique_ids = list(dict.fromkeys(employee_ids))
print(unique_ids)
[101, 102, 103, 104]
5. Python Dictionaries – Key-Value Data Storage
A dictionary stores data as key-value pairs. Each key maps to a value, allowing us to retrieve information using a meaningful identifier instead of a numeric position.
For example, an employee record can store an employee ID, name, department and salary under descriptive keys.
employee = {
"id": 102,
"name": "Rahul",
"department": "Engineering",
"salary": 85000
}
print(employee["name"])
print(employee["department"])
print(employee["salary"])
Rahul Engineering 85000
5.1 Accessing Dictionary Values
You can access a value using square brackets or the get() method.
employee = {
"id": 102,
"name": "Rahul"
}
print(employee["name"])
print(employee.get("department"))
print(employee.get("department", "Not Assigned"))
Rahul None Not Assigned
employee["department"] raises KeyError. Using employee.get("department") returns None by default, or a fallback value if you provide one.5.2 Adding, Updating and Deleting Dictionary Values
employee = {
"id": 102,
"name": "Rahul"
}
# Add a new key
employee["department"] = "Engineering"
# Update an existing key
employee["name"] = "Rahul Sharma"
# Add or update multiple values
employee.update({
"salary": 85000,
"location": "Bengaluru"
})
# Delete a key
del employee["location"]
print(employee)
{'id': 102, 'name': 'Rahul Sharma',
'department': 'Engineering', 'salary': 85000}
5.3 Iterating Through a Dictionary
employee = {
"id": 102,
"name": "Rahul",
"department": "Engineering"
}
for key in employee:
print(key)
for value in employee.values():
print(value)
for key, value in employee.items():
print(key, ":", value)
id name department 102 Rahul Engineering id : 102 name : Rahul department : Engineering
5.4 Dictionary Keys Must Be Hashable
Dictionary keys must be hashable. Common immutable types such as strings, integers and suitable tuples can be used as keys. Lists and sets cannot be used as dictionary keys because they are mutable and unhashable.
# Valid keys
records = {
"employee": "Rahul",
101: "Active",
(10, 20): "Coordinates"
}
# Invalid: list cannot be a dictionary key
# records[[1, 2]] = "Invalid"
print(records["employee"])
print(records[101])
Rahul Active
6. Mutability – A Concept You Must Understand
Mutability describes whether an object can be changed after it has been created. This is one of the most important differences between Python's collection types.
| Collection | Mutable? | Ordered? | Duplicates? |
|---|---|---|---|
| List | Yes | Yes | Allowed |
| Tuple | No | Yes | Allowed |
| Set | Yes | No positional order | Not allowed |
| Dictionary | Yes | Yes – insertion order | Keys must be unique |
6.1 Mutable Objects and References
When you assign a list to another variable, Python does not automatically create a separate list. Both variables refer to the same object.
first = [10, 20, 30]
second = first
second.append(40)
print(first)
print(second)
print(first is second)
[10, 20, 30, 40] [10, 20, 30, 40] True
6.2 Shallow Copy and Deep Copy
A shallow copy creates a new outer collection, but nested objects are still shared. A deep copy recursively copies nested objects as well.
import copy
original = {
"name": "Ananya",
"skills": ["Python", "SQL"]
}
shallow = copy.copy(original)
deep = copy.deepcopy(original)
original["skills"].append("AWS")
print(shallow["skills"])
print(deep["skills"])
['Python', 'SQL', 'AWS'] ['Python', 'SQL']
The shallow copy shares the nested list with the original. The deep copy has an independent nested list.
copy.deepcopy() when nested mutable objects must be copied independently. Deep copying can be more expensive and may not be appropriate for every object.7. List Comprehensions and Dictionary Comprehensions
Comprehensions provide a concise way to create collections from existing iterables. They are especially useful for filtering and transforming data.
7.1 List Comprehension
numbers = [1, 2, 3, 4, 5, 6]
# Traditional approach
squares = []
for number in numbers:
squares.append(number ** 2)
# Comprehension approach
square_values = [number ** 2 for number in numbers]
print(square_values)
[1, 4, 9, 16, 25, 36]
7.2 Filtering with List Comprehension
numbers = [10, 15, 20, 25, 30, 35]
even_numbers = [
number for number in numbers
if number % 2 == 0
]
print(even_numbers)
[10, 20, 30]
7.3 Dictionary Comprehension
products = ["Laptop", "Mouse", "Keyboard"]
product_codes = {
product: len(product)
for product in products
}
print(product_codes)
{'Laptop': 6, 'Mouse': 5, 'Keyboard': 8}
7.4 Set Comprehension
words = ["Python", "Java", "SQL", "Ruby", "CSharp"]
lengths = {len(word) for word in words}
print(lengths)
{3, 4, 6} # Set display order may vary
8. Nested Data Structures
In real applications, data is often hierarchical. A dictionary may contain lists, lists may contain dictionaries, and dictionaries may contain other dictionaries.
For example, an order management application may store an order with multiple products, quantities and prices.
order = {
"order_id": "ORD-501",
"customer": {
"name": "Ananya",
"city": "Bengaluru"
},
"items": [
{"product": "Laptop", "quantity": 1, "price": 65000},
{"product": "Mouse", "quantity": 2, "price": 800}
]
}
print(order["customer"]["name"])
print(order["items"][0]["product"])
total = sum(
item["quantity"] * item["price"]
for item in order["items"]
)
print("Order Total:", total)
Ananya Laptop Order Total: 66600
8.1 Understanding the Data Structure
9. Choosing the Right Data Structure
There is no single best data structure for every problem. The right choice depends on how your application accesses and changes its data.
| Requirement | Recommended structure | Reason |
|---|---|---|
| Maintain an ordered collection | List | Supports indexing and modification |
| Store fixed sequence values | Tuple | Prevents element reassignment |
| Remove duplicates | Set | Stores unique hashable elements |
| Fast key-based lookup | Dictionary | Average-case efficient key lookup |
| Check whether an item exists | Set or Dictionary | Average-case efficient membership checks |
| Store structured records | Dictionary or class | Descriptive fields and relationships |
10. Time Complexity of Python Data Structures
Understanding time complexity helps you estimate how an operation behaves as the collection grows. The following are typical average-case complexities for Python's built-in list, set and dictionary operations.
| Operation | List | Tuple | Set | Dictionary |
|---|---|---|---|---|
| Access by index | O(1) | O(1) | Not supported | Not applicable |
| Search by value/key | O(n) | O(n) | O(1) average | O(1) average by key |
| Append / add | O(1) amortized | Not supported | O(1) average | O(1) average for insertion |
| Remove value | O(n) | Not supported | O(1) average | O(1) average by key |
| Iteration | O(n) | O(n) | O(n) | O(n) |
10.1 Why Is Dictionary Lookup Usually Fast?
A dictionary uses a hash table. Python calculates a hash value for a key and uses it to locate the corresponding entry. Hash collisions are handled internally by the implementation.
11. Practical Example – Employee Skill Management
Let's combine lists, sets and dictionaries in a practical employee skill management example. The application needs to store employee information, identify unique skills, and find employees who know a particular technology.
employees = [
{
"id": 101,
"name": "Ananya",
"skills": ["Python", "SQL", "AWS"]
},
{
"id": 102,
"name": "Rahul",
"skills": ["Python", "Docker", "SQL"]
},
{
"id": 103,
"name": "Kiran",
"skills": ["Java", "AWS", "Docker"]
}
]
# Collect all unique skills
all_skills = {
skill
for employee in employees
for skill in employee["skills"]
}
# Find employees with Python skills
python_developers = [
employee["name"]
for employee in employees
if "Python" in employee["skills"]
]
# Create a lookup dictionary using employee ID
employee_lookup = {
employee["id"]: employee
for employee in employees
}
print("Unique Skills:", sorted(all_skills))
print("Python Developers:", python_developers)
print("Employee 102:", employee_lookup[102]["name"])
Unique Skills: ['AWS', 'Docker', 'Java', 'Python', 'SQL'] Python Developers: ['Ananya', 'Rahul'] Employee 102: Rahul
What Did We Use?
The employee records are stored in a list because we need an ordered collection that can be iterated over.
Each employee is represented as a dictionary with descriptive keys such as
id, name and skills.A set comprehension collects unique skills without manually checking for duplicates.
We build an employee lookup dictionary so that a record can be retrieved using its ID.
12. Common Mistakes to Avoid
12.1 Confusing append() and extend()
Use append() to add one element. Use extend() to add elements from an iterable.
12.2 Assuming a Set Has Indexes
Sets do not support positional access. Convert a set to a list only when you need sequence operations, and do not assume the conversion provides a meaningful order.
12.3 Modifying a Collection While Iterating
Changing a list or dictionary during iteration can cause skipped elements or runtime errors. When filtering a list, create a new list or iterate over a copy.
numbers = [10, 15, 20, 25, 30]
# Create a new filtered list
numbers = [
number for number in numbers
if number % 2 == 0
]
print(numbers)
[10, 20, 30]
12.4 Using a Mutable Default Argument
Default argument values are evaluated when the function is defined, not each time it is called. Avoid using a mutable list or dictionary as a default when you intend a fresh collection for every call.
def add_skill(skill, skills=None):
if skills is None:
skills = []
skills.append(skill)
return skills
first = add_skill("Python")
second = add_skill("SQL")
print(first)
print(second)
['Python'] ['SQL']
12.5 Assuming a Copy Creates Independent Nested Data
A shallow copy does not recursively copy nested objects. Use a deep copy only when independent nested objects are required.
13. Interview Questions and Answers
remove(value) removes the first matching value. pop(index) removes and returns the element at the specified index; without an index, it removes the last element.14. Hands-On Practice
15. Best Practices
- Choose a collection based on the operations your application performs most frequently.
- Use lists for ordered, changeable sequences.
- Use tuples for fixed groups of values.
- Use sets for uniqueness and membership checks.
- Use dictionaries for key-based lookups and structured records.
- Use descriptive dictionary keys instead of unexplained numeric positions.
- Avoid unnecessary conversions between collection types.
- Be aware of shared references when assigning or copying mutable objects.
- Use comprehensions when they improve clarity, not simply to shorten code.
- Consider memory usage and time complexity when processing large collections.
16. Key Takeaways
Python data structures are fundamental to writing practical applications. Understanding their characteristics helps you choose the right collection for a particular problem.
- List: Ordered, mutable and allows duplicates.
- Tuple: Ordered, immutable and allows duplicates.
- Set: Stores unique hashable values and supports set operations.
- Dictionary: Stores key-value pairs and preserves insertion order.
- Comprehensions: Provide concise ways to construct collections.
- Mutability: Determines whether an existing object can be changed.
- Time complexity: Helps you select efficient operations for larger datasets.
Now that you understand Python's core data structures, practise combining them in real applications. Work with nested dictionaries, collections of records, filtering, grouping and lookup operations. These concepts form the foundation for functions, object-oriented programming, APIs and data processing in Python.
Upgradix by Shrash Studio – Learn Technology. Build Your Future.