HomeΒ Β Python Β Β Top 5 most ...

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.

# 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.

# 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.

# 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.

# 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 StructureOrderedMutableAllows DuplicatesTypical Use
Listβœ… Yesβœ… Yesβœ… YesGeneral storage, iteration
Tupleβœ… Yes❌ Noβœ… YesFixed data (e.g., coordinates)
Dictionaryβœ… Yesβœ… YesπŸ”‘ Keys uniqueKey–value mapping
Set❌ Noβœ… Yes❌ NoUnique elements, membership
Dequeβœ… Yesβœ… Yesβœ… YesQueues, fast append/pop

Published on: Oct 19, 2025, 08:53 AM Β 
Β 

Comments

Add your comment