Types of Data Structures in Python
1. Built-in Data Structures
Primitive Types: int, float, str, bool
Collections: list, tuple, dict, set
Classes: Objects and custom types
Advanced Structures: Linked lists, trees, graphs, stacks, queues, etc.
Why are Data Structures important?
Efficient Data Management: Store and access data quickly and efficiently.
Solve Complex Problems: Algorithms often require specific data structures for optimal performance.
Scalability: Handle large-scale data in real-world applications like databases, networking, etc.
1. Lists (Dynamic Arrays)
Definition: Ordered, mutable, and can hold elements of different types.
Real-Life Analogy: A shopping list where you can add, remove, or modify items.
my_list = [1, 2, 3, "hello"] # Add my_list.append(4) # [1, 2, 3, "hello", 4] # Remove my_list.remove(2) # [1, 3, "hello", 4] # Access print(my_list[1]) # 3 # Iteration for item in my_list: print(item)
2. Tuples (Immutable Arrays)
Definition: Ordered, immutable, and often used for fixed collections of items.
Real-Life Analogy: Latitude and longitude coordinates that shouldn’t change.
coordinates = (40.7128, -74.0060) print(coordinates[0]) # 40.7128
3. Dictionaries (Hash Maps)
Definition: Key-value pairs, unordered, and mutable.
Real-Life Analogy: A phonebook where names (keys) map to numbers (values).
phonebook = {"Alice": "123-456", "Bob": "987-654"} # Add phonebook["Charlie"] = "555-555" # Access print(phonebook["Alice"]) # 123-456 # Iterate for name, number in phonebook.items(): print(f"{name}: {number}")
4. Sets
Definition: Unordered, mutable, and no duplicate elements.
Real-Life Analogy: A bag of unique items like a deck of cards without repeated cards.
cards = {"Ace", "King", "Queen"} cards.add("Jack") # {"Ace", "King", "Queen", "Jack"} cards.remove("Ace") # {"King", "Queen", "Jack"}
5. Stacks (LIFO)
Definition: Last In, First Out data structure.
Real-Life Analogy: A stack of plates where you add/remove from the top.
stack = [] stack.append("Plate1") # Add stack.append("Plate2") stack.pop() # Remove top ("Plate2")
6. Queues (FIFO)
Definition: First In, First Out data structure.
Real-Life Analogy: People standing in line for a ticket.
from collections import deque queue = deque(["Alice", "Bob"]) queue.append("Charlie") # Add to back queue.popleft() # Remove front ("Alice")
7. Linked Lists
Definition: A collection of nodes, where each node points to the next.
Real-Life Analogy: A train where each compartment (node) is connected to the next.
class Node: def __init__(self, data): self.data = data self.next = None class LinkedList: def __init__(self): self.head = None def append(self, data): new_node = Node(data) if not self.head: self.head = new_node else: current = self.head while current.next: current = current.next current.next = new_node def display(self): current = self.head while current: print(current.data, end=" -> ") current = current.next print("None") # Usage ll = LinkedList() ll.append(1) ll.append(2) ll.append(3) ll.display() # Output: 1 -> 2 -> 3 -> None
8. Trees
Definition: Hierarchical structure with a root and child nodes.
Real-Life Analogy: File directory system with folders and subfolders.
class TreeNode: def __init__(self, value): self.value = value self.left = None self.right = None def inorder_traversal(root): if root: inorder_traversal(root.left) print(root.value, end=" ") inorder_traversal(root.right) # Create Tree root = TreeNode(10) root.left = TreeNode(5) root.right = TreeNode(20) inorder_traversal(root) # Output: 5 10 20
9. Graphs
Definition: Nodes connected by edges, representing relationships.
Real-Life Analogy: A map where cities (nodes) are connected by roads (edges).
graph = { "A": ["B", "C"], "B": ["A", "D"], "C": ["A", "D"], "D": ["B", "C"] } # Print connections for node, neighbors in graph.items(): print(f"{node}: {', '.join(neighbors)}")
10. Heaps
Definition: Specialized tree-based structure that maintains the heap property.
Real-Life Analogy: Priority queues (e.g., hospital ER queue by urgency).
import heapq heap = [] heapq.heappush(heap, 3) # Add heapq.heappush(heap, 1) heapq.heappush(heap, 5) print(heapq.heappop(heap)) # Remove smallest (1)
Choosing the Right Data Structure
Each data structure has specific use cases based on its properties. Here's a quick guide:
Key Real-Life Applications
Here are some real-world applications of data structures:
- Lists: Manage tasks in a to-do list app.
- Dictionaries: Store user details in a web app.
- Sets: Filter duplicate requests in a web server.
- Stacks/Queues: Browser history (stack) or task scheduling (queue).
- Trees: Represent JSON/XML data or file systems.
- Graphs: Model social networks or road maps.
- Heaps: Implement priority queues.
Conclusion
Understanding data structures is essential for writing efficient code and solving complex problems. Python provides a rich set of built-in and user-defined structures to handle various scenarios. Choose the right structure based on your use case to optimize performance and maintainability.
Recommended Data Structures
Based on your use case, here are some recommended data structures:
Use Case | Recommended Structure |
---|---|
Maintain order | list, deque |
Lookup by key | dict |
Fast membership tests | set |
LIFO operations | stack |
FIFO operations | queue |
Dynamic data with references | linked list |
Hierarchical data | tree |
Network/Relationship modelling | graph |
Priority-based access | heap |