Skip to content
derpx06.dev
WritingExplainersNotesAbout
works on my machineit's always DNSship itLGTMTODO: sleepoff by oneread the docsgit push --force-with-lease404: sleep not foundhave you tried turning it off and on againrubber duck says: explain itpremature optimisationworks on my machineit's always DNSship itLGTMTODO: sleepoff by oneread the docsgit push --force-with-lease404: sleep not foundhave you tried turning it off and on againrubber duck says: explain itpremature optimisation

Learning in public.

A student's notebook on system design, distributed systems and databases. Shared as I learn.

ReadWritingExplainersNotesAbout
ElsewhereGitHubLinkedInHugging FaceLeetCode
Say hellomanaskhairnar1511@gmail.com
derpx06
NORMALmain© 2026 Derpx06Pune, Indiatop ↑
Learn/Python Overview/Part 5 · 5. Data Work
Lesson 16 of 17

Iterators & Zip

enumerate, zip, and argument unpacking.

Derpx061 Jan · 1 min read
Python Overview
RandomnessRegular ExpressionsObject-Oriented ProgrammingIterators & ZipArgs & Kwargs
Contents
enumeratezip and Argument Unpacking
0% read · 1 min leftBack to top ↑
Course outline & contents0%
Python Overview
RandomnessRegular ExpressionsObject-Oriented ProgrammingIterators & ZipArgs & Kwargs
Contents
enumeratezip and Argument Unpacking

enumerate

Not infrequently, you’ll want to iterate over a list and use both its elements and their indexes:

not_pythonic.py
# not Pythonic
for i in range(len(documents)):
  document = documents[i]
  do_something(i, document)

# also not Pythonic
i = 0
for document in documents:
  do_something(i, document)
  i += 1

The Pythonic solution is enumerate, which produces tuples (index, element):

enumerate.py
for i, document in enumerate(documents):
  do_something(i, document)

Similarly, if we just want the indexes:

indexes.py
for i in range(len(documents)): do_something(i)   # not Pythonic
for i, _ in enumerate(documents): do_something(i) # Pythonic

We’ll use this a lot.

zip and Argument Unpacking

Often we will need to zip two or more lists together. zip transforms multiple lists into a single list of tuples of corresponding elements:

zip.py
list1 = ['a', 'b', 'c']
list2 = [1, 2, 3]
zip(list1, list2)   # is [('a', 1), ('b', 2), ('c', 3)]

If the lists are different lengths, zip stops as soon as the first list ends.

You can also "unzip" a list using a strange trick:

unzip.py
pairs = [('a', 1), ('b', 2), ('c', 3)]
letters, numbers = zip(*pairs)

The asterisk performs argument unpacking, which uses the elements of pairs as individual arguments to zip. It ends up the same as if you’d called:

zipping_tuples.py
zip(('a', 1), ('b', 2), ('c', 3))

which returns [('a','b','c'), ('1','2','3')].

You can use argument unpacking with any function:

unpacking.py
def add(a, b): return a + b

add(1, 2)       # returns 3
add([1, 2])     # TypeError!
add(*[1, 2])    # returns 3

It is rare that we’ll find this useful, but when we do it’s a neat trick.

Track your progressSign in and your progress follows you across devices.
Sign in to track progress Next lesson
← Previous lessonObject-Oriented ProgrammingNext lesson →Args & Kwargs