SwadhyAI Notes

All Python notes

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.

FeaturePython ListArray (module)
TypeHeterogeneous (Mixed types)Homogeneous (Same type)
FlexibilityHighLow
PerformanceSlower (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!