This content originally appeared on DEV Community and was authored by Aditi Sharma
Today, I deep-dived into Lists in Python and two powerful functions β zip() and enumerate().
What are Lists?
Lists are ordered, mutable collections that can store multiple items.
fruits = [“apple”, “banana”, “cherry”]
print(fruits) # [‘apple’, ‘banana’, ‘cherry’]
Some useful methods:
β’ append() β add an element
β’ insert() β insert at a specific index
β’ remove() β remove an element
β’ sort() β sort elements
β’ reverse() β reverse the order
enumerate() β cleaner looping
Instead of using a counter, enumerate() gives both index and value.
fruits = [“apple”, “banana”, “cherry”]
for index, fruit in enumerate(fruits):
print(index, fruit)
Output:
0 apple
1 banana
2 cherry
zip() β pairing data
When you want to iterate over multiple lists at once, zip() is a life-saver.
names = [“Alice”, “Bob”, “Charlie”]
scores = [85, 90, 95]
for name, score in zip(names, scores):
print(f”{name}: {score}”)
Output:
Alice: 85
Bob: 90
Charlie: 95
Why this matters?
β’ Lists are one of the most fundamental data structures in Python.
β’ enumerate() improves readability.
β’ zip() simplifies working with parallel data.
This content originally appeared on DEV Community and was authored by Aditi Sharma