Lists and Arrays in Python đ
Dynamic Arrays
In Python, **Lists** act as dynamic arrays. They can hold items of different types and resize automatically.
⨠Python Lists vs Arrays
While Python has a built-in `array` module, Lists are far more common and versatile.
| Feature | Python List | Array (module) |
|---|---|---|
| Type | Heterogeneous (Mixed types) | Homogeneous (Same type) |
| Flexibility | High | Low |
| Performance | Slower (more overhead) | Faster (memory efficient) |
đŽ Interactive List Demo
âšī¸ Ready to explore Python Lists!
Apple0
Banana1
Cherry2
đ Common Operations
python
# Creating a list
my_list = [1, 2, 3, "Hello"]
# Appending (O(1) amortized)
my_list.append(4)
# Inserting (O(n))
my_list.insert(0, 0)
# Accessing (O(1))
print(my_list[2])
# Popping (O(1) from end, O(n) from start)
my_list.pop()â ī¸ Watch Out!
Inserting or deleting elements from the beginning of a list is **O(n)** because all other elements must shift. For a queue (FIFO), use `collections.deque` instead!