Skip to content

Data Structures in Python

Data structures are ways to organize and store data efficiently. Python provides several built-in data structures:

List

A dynamic array for ordered data.

numbers = [1, 2, 3]

Tuple

Immutable, ordered data.

point = (1, 2)

Set

Unordered, unique items.

unique = {1, 2, 3}

Dictionary

Key-value pairs.

person = {"name": "Alice", "age": 30}

Stack

Use a list with append() and pop().

Queue

Use collections.deque for efficient FIFO.

Example: Stack

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

Resources