# Python Data Structures

Table of Contents

Python includes a rich set of built-in data structures. This cheatsheet covers the core types, their use cases, and examples.

Lists

Ordered, mutable sequences.

fruits = ["apple", "banana", "cherry"]
fruits.append("date")
print(fruits[1]) # banana

When to use: Ordered collections with frequent insertions, deletions, or updates.

Tuples

Ordered, immutable sequences.

point = (3, 4)
print(point[0]) # 3

When to use: Fixed collections, function return values, or dictionary keys.

Sets

Unordered collections of unique elements.

colors = {"red", "green", "blue"}
colors.add("yellow")
print("red" in colors)

When to use: Membership tests, eliminating duplicates, set operations (union, intersection).

Dictionaries

Key-value mappings.

person = {"name": "Alice", "age": 30}
print(person["name"])
person["city"] = "New York"

When to use: Fast lookups by unique keys, mapping relationships.

Strings

Immutable sequences of characters.

greeting = "Hello, World"
print(greeting.lower())

When to use: Text processing, immutable sequences.

Stacks (via list)

stack = []
stack.append(1)
stack.append(2)
print(stack.pop()) # 2

When to use: Last-in, first-out (LIFO) data handling.

Queues

Use collections.deque for efficiency.

from collections import deque
queue = deque([1, 2, 3])
queue.append(4)
print(queue.popleft()) # 1

When to use: First-in, first-out (FIFO) tasks.

Heaps (Priority Queues)

import heapq
heap = [3, 1, 4]
heapq.heapify(heap)
heapq.heappush(heap, 2)
print(heapq.heappop(heap)) # 1

When to use: Retrieve smallest/largest items efficiently.

Arrays

For numeric data, use array or numpy for performance.

from array import array
nums = array('i', [1, 2, 3])

When to use: Memory-efficient storage of large numeric data.

Choosing the Right Structure

StructureOrderedMutableDuplicates
listYesYesYes
tupleYesNoYes
setNoYesNo
dictNo*YesKeys: No

*Insertion order preserved from Python 3.7+.

Final Thoughts

Knowing Python’s built-in data structures and their performance characteristics helps you write clean, efficient code.

My avatar

Thanks for reading my blog post! Feel free to check out my other posts or contact me via the social links in the footer.


More Posts