top 5 most used data structures in Python
Here are the top 5 most used data structures in Python, with explanations and examples π
π₯ 1. List
β Dynamic array that can store elements of any type.
- Mutable (can be changed)
- Ordered
# Create a list
fruits = ["apple", "banana", "cherry"]
# Access and modify
print(fruits[0]) # apple
fruits.append("mango") # add element
fruits.remove("banana") # remove element
print(fruits) # ['apple', 'cherry', 'mango']
π₯ 2. Tuple
β Immutable version of list β once created, canβt be changed.
- Useful for fixed data, like coordinates or config constants.
- Faster than lists.
# Create a tuple
point = (10, 20)
# Access elements
print(point[0]) # 10
# You cannot modify a tuple
# point[0] = 15 β (Error)
π₯ 3. Dictionary (HashMap)
β Keyβvalue store, very similar to JavaScript objects or Java HashMaps.
- Unordered (before Python 3.7), now insertion ordered
- Mutable
# Create a dictionary
person = {"name": "Alice", "age": 25}
# Access or update
print(person["name"]) # Alice
person["city"] = "Sydney" # Add new key
print("age" in person) # True
# Iterate
for key, value in person.items():
print(key, value)
π 4. Set
β Unordered collection of unique elements.
- Removes duplicates automatically
- Useful for membership testing and set operations.
# Create a set
nums = {1, 2, 3, 2, 1}
print(nums) # {1, 2, 3}
# Add or remove
nums.add(4)
nums.remove(2)
# Set operations
a = {1, 2, 3}
b = {3, 4, 5}
print(a | b) # Union: {1, 2, 3, 4, 5}
print(a & b) # Intersection: {3}
ποΈ 5. Queue / Deque
β
For FIFO (First-In, First-Out) or LIFO operations.
Pythonβs collections.deque is faster than list for queue operations.
from collections import deque
queue = deque(["A", "B", "C"])
queue.append("D") # Enqueue
print(queue.popleft()) # Dequeue -> A
print(queue) # deque(['B', 'C', 'D'])
β‘ Summary Table
| Data Structure | Ordered | Mutable | Allows Duplicates | Typical Use |
|---|---|---|---|---|
| List | β Yes | β Yes | β Yes | General storage, iteration |
| Tuple | β Yes | β No | β Yes | Fixed data (e.g., coordinates) |
| Dictionary | β Yes | β Yes | π Keys unique | Keyβvalue mapping |
| Set | β No | β Yes | β No | Unique elements, membership |
| Deque | β Yes | β Yes | β Yes | Queues, fast append/pop |
Published on: Oct 19, 2025, 08:53 AM Β
Β