LATEST 🚀 Welcome to Upgradix by Shrash Studio – Your destination for free technology learning and career growth.
🕘 Updated:
Python

Python - Data Structures in Depth

When writing programs, we often need to store collections of related items rather than single variables. Data structures are specialized formats for organizing, processing, and storing data efficiently in computer memory.

Python – Data Structures in Depth

Quick Answer

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.

Python – storing employee names
employee_1 = "Ananya"
employee_2 = "Rahul"
employee_3 = "Kiran"

# A list is easier to manage
employees = ["Ananya", "Rahul", "Kiran"]

print(employees)
Expected Output
['Ananya', 'Rahul', 'Kiran']

Python provides several built-in data structures. The four most commonly used collection types are:

The Four Core Python Collection Types
Python Collections LIST Ordered Mutable TUPLE Ordered Immutable SET Unique values Unordered DICTIONARY Key-value Mutable Choose a collection based on how you need to access and manage data.
Each collection solves a different data organisation problem.

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.

Creating a list
products = ["Laptop", "Keyboard", "Mouse"]

print(products)
print(products[0])
print(products[-1])
print(len(products))
Expected Output
['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.

List indexing and slicing
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
Expected Output
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

Adding, updating and removing elements
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)
Expected Output
['Laptop', 'Wireless Mouse', 'Keyboard', 'Monitor']
Removed: Monitor
Important: append() adds its argument as a single element. extend() iterates over the supplied iterable and adds its elements individually.
append() versus extend()
first = [1, 2]
first.append([3, 4])
print(first)

second = [1, 2]
second.extend([3, 4])
print(second)
Expected Output
[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.

Creating tuples
coordinates = (15.1394, 76.9214)
rgb_color = (255, 120, 50)

print(coordinates[0])
print(coordinates[1])
print(rgb_color)
Expected Output
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.

Correct and incorrect tuple creation
not_a_tuple = (10)
single_item_tuple = (10,)

print(type(not_a_tuple))
print(type(single_item_tuple))
Expected Output
<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.

Tuple unpacking
employee = ("E102", "Ananya", "Engineering")

employee_id, name, department = employee

print(employee_id)
print(name)
print(department)
Expected Output
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.
Remember: A tuple is immutable, but an object stored inside it may itself be mutable. For example, a tuple can contain a list, and that list can still be modified.
Tuple containing a mutable list
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)
Expected Output
('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.

Creating a set
departments = {"HR", "IT", "Finance", "IT", "HR"}

print(departments)
print(len(departments))
Expected Output
The set contains the unique values:
{'HR', 'IT', 'Finance'}  # Display order may vary
3

4.1 Adding and Removing Set Elements

Set methods
skills = {"Python", "SQL"}

skills.add("AWS")
skills.update(["Docker", "Git"])

skills.discard("Java")
skills.remove("SQL")

print(skills)
Expected Output
A set containing:
Python, AWS, Docker and Git
The iteration order may vary.

Two commonly used removal methods are discard() and remove().

  • remove(value) raises KeyError if 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.

Union, intersection and difference
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)
Expected Output
Union: Ananya, Rahul, Kiran, Meera
Intersection: Rahul, Kiran
Difference: Ananya
Symmetric difference: Ananya, Meera

Set display order may vary.
How Set Operations Work
Set A Set B A only Common B only Union = all • Intersection = overlap • Difference = one side only
A conceptual view of two sets and their shared elements.

4.3 Removing Duplicates from a List

One common use case for sets is removing duplicate values from a list.

Removing duplicate employee IDs
employee_ids = [101, 102, 101, 103, 102, 104]

unique_ids = set(employee_ids)

print(unique_ids)
Expected Output
{101, 102, 103, 104}
Set display order may vary.

If the original insertion order must be preserved, use an order-preserving approach:

Remove duplicates while preserving order
employee_ids = [101, 102, 101, 103, 102, 104]

unique_ids = list(dict.fromkeys(employee_ids))

print(unique_ids)
Expected Output
[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.

Creating a dictionary
employee = {
    "id": 102,
    "name": "Rahul",
    "department": "Engineering",
    "salary": 85000
}

print(employee["name"])
print(employee["department"])
print(employee["salary"])
Expected Output
Rahul
Engineering
85000

5.1 Accessing Dictionary Values

You can access a value using square brackets or the get() method.

Square brackets versus get()
employee = {
    "id": 102,
    "name": "Rahul"
}

print(employee["name"])
print(employee.get("department"))
print(employee.get("department", "Not Assigned"))
Expected Output
Rahul
None
Not Assigned
Practical difference: Accessing a missing key with 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

Dictionary modifications
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)
Expected Output
{'id': 102, 'name': 'Rahul Sharma',
 'department': 'Engineering', 'salary': 85000}

5.3 Iterating Through a Dictionary

Keys, values and key-value pairs
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)
Expected Output
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 and invalid dictionary keys
# 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])
Expected Output
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.

Understanding object references
first = [10, 20, 30]
second = first

second.append(40)

print(first)
print(second)
print(first is second)
Expected Output
[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.

Shallow copy versus deep copy
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"])
Expected Output
['Python', 'SQL', 'AWS']
['Python', 'SQL']

The shallow copy shares the nested list with the original. The deep copy has an independent nested list.

When to use: Use a shallow copy when sharing nested objects is acceptable. Use 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

Traditional loop and 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)
Expected Output
[1, 4, 9, 16, 25, 36]

7.2 Filtering with List Comprehension

Find even numbers
numbers = [10, 15, 20, 25, 30, 35]

even_numbers = [
    number for number in numbers
    if number % 2 == 0
]

print(even_numbers)
Expected Output
[10, 20, 30]

7.3 Dictionary Comprehension

Create a dictionary from a list
products = ["Laptop", "Mouse", "Keyboard"]

product_codes = {
    product: len(product)
    for product in products
}

print(product_codes)
Expected Output
{'Laptop': 6, 'Mouse': 5, 'Keyboard': 8}

7.4 Set Comprehension

Create a set of unique word lengths
words = ["Python", "Java", "SQL", "Ruby", "CSharp"]

lengths = {len(word) for word in words}

print(lengths)
Expected Output
{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.

Nested dictionary and list
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)
Expected Output
Ananya
Laptop
Order Total: 66600

8.1 Understanding the Data Structure

Nested Order Data – Structure View
order (dictionary) order_id : "ORD-501" customer (dict) items (list) name : Ananya city : Bengaluru Item 0 Laptop Item 1 Mouse A dictionary can contain other dictionaries and lists.
The order object organises related information into nested collections.

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)
Complexity note: Set and dictionary operations are generally O(1) on average because they use hash tables. Their worst-case behaviour can be O(n). List insertion or deletion near the beginning or middle is O(n) because elements may need to shift.

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.

Conceptual Dictionary Lookup
Key "employee_id" Hash Function hash(key) Hash Table Index Value Record Simplified illustration – Python manages hashing and collision handling internally.
A simplified view of how a dictionary locates a value using its key.

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.

Complete Python 3 example
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"])
Expected Output
Unique Skills: ['AWS', 'Docker', 'Java', 'Python', 'SQL']
Python Developers: ['Ananya', 'Rahul']
Employee 102: Rahul

What Did We Use?

List
The employee records are stored in a list because we need an ordered collection that can be iterated over.
Dictionary
Each employee is represented as a dictionary with descriptive keys such as id, name and skills.
Set
A set comprehension collects unique skills without manually checking for duplicates.
Dictionary comprehension
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.

Safer filtering approach
numbers = [10, 15, 20, 25, 30]

# Create a new filtered list
numbers = [
    number for number in numbers
    if number % 2 == 0
]

print(numbers)
Expected Output
[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.

Recommended function pattern
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)
Expected Output
['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

Q1. What is the difference between a list and a tuple? A list is mutable, whereas a tuple is immutable. Both preserve order, support indexing and allow duplicate values.
Q2. Why would you use a set instead of a list? Use a set when you need unique values or frequent membership checks. Set membership is O(1) on average, while list membership is O(n).
Q3. Are dictionaries ordered in Python? Yes. Python dictionaries preserve insertion order as a language guarantee from Python 3.7 onward.
Q4. Can a list be used as a dictionary key? No. Lists are mutable and unhashable. Dictionary keys must be hashable.
Q5. Can a tuple contain a list? Yes. A tuple can contain a list, but the nested list remains mutable even though the tuple itself is immutable.
Q6. What is the difference between remove() and pop() in a list? 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.
Q7. What is the difference between shallow copy and deep copy? A shallow copy creates a new outer object but shares nested references. A deep copy recursively copies nested objects.
Q8. What is a hashable object? A hashable object has a hash value that remains constant during its lifetime and can be compared for equality. Hashable objects can be used as set elements and dictionary keys.
Q9. What happens when duplicate keys are used in a dictionary literal? The later value replaces the earlier value for that key.
Q10. What is the difference between a list comprehension and a generator expression? A list comprehension creates a list immediately. A generator expression produces values lazily as they are requested, which can reduce memory usage for large sequences.

14. Hands-On Practice

Challenge 1 – Remove Duplicates Given a list of transaction IDs, remove duplicate IDs while preserving their original order. ListDictionary
Challenge 2 – Find Common Skills Given two sets of employee skills, find the skills common to both employees. SetIntersection
Challenge 3 – Product Lookup Create a dictionary containing product IDs and product names. Retrieve a product using its ID and handle missing IDs safely. DictionaryLookup
Challenge 4 – Salary Analysis Store employee records in a list of dictionaries. Find the employee with the highest salary and calculate the average salary. ListDictionary
Challenge 5 – Group Employees by Department Given a list of employee dictionaries, build a dictionary where each department maps to a list of employee names. Nested CollectionsComprehensions

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.
What to Learn Next

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.

Comments & Ratings (0)

Log in or create a student account to comment and rate this article.

Be the first to comment on this article.