Member-only story
Grok Python’s Zip Function
Python is full of surprises that make programming fun and extremely efficient, on your desktop, laptop, or even using today’s latest calculators.
When I first started learning Python it took me a while to discover the zip() function. Of course that’s the nature of Python; there’s always something new to learn about this language. Most of the time these new Python discoveries are awesome, powerful, and easy to use once you grok them, and that’s definitely true of zip!
So, how does the zip function work? Concisely worded, zip() brings elements of the same index from multiple iterable objects together as tuple elements of an iterable zip object. Let’s unpack that definition with an example, because working examples are often much easier to understand than words alone. Here’s a short code example to get the grokking started:
a = ["one", "two", "three"]
b = ["apples", "oranges", "peaches"]c = zip(a, b)print(c)
# <zip object at 0x000001E6BF050380>
Zip Returns an Iterable Object, but That’s Okay!
Notice that the value of c is not a tuple or list or other common variable type, it is an iterable zip object as shown in the output, so we need to iterate through it to use it. This is very easy to do, in several ways, and…