concept zip in category data

This is an excerpt from Manning's book Mastering Large Datasets with Python: Parallelize and Distribute Your Python Code.
zip is another lazy function. We use zip when we have two iterables that we want to join together so that the items in the first position are together, the items in the second position are together, and so on. Naturally, it makes sense to think about the zip function like a zipper. When the zipper passes over each pair of teeth, it pulls them together into a pair (figure 4.2).
Figure 4.2. The zip function behaves like a zipper, but instead of interlocking metal teeth, it interlocks the values of Python iterables.
![]()
We use the zip function when we have related sequences that we want to bring together. For instance, if an ice cream vendor knows how many ice cream cones they’ve sold in the last two weeks, they may be interested in zipping that together with the temperature to analyze if there are any trends, as shown in the following listing.
Listing 4.3. Ice cream data and the zip function
ice_cream_sales = [27, 21, 39, 31, 12, 40, 11, 18, 30, 19, 24, 35, 31, 12] temperatures = [75, 97, 88, 99, 81, 92, 91, 84, 84, 93, 100, 86, 90, 75] ice_cream_data = zip(ice_cream_sales, temperatures) print(list(ice_cream_data)) # [(27,75), (21,97), (39,88), ... (12,75)]Having data paired in tuples like that is helpful because tuples can easily be passed to functions and unpacked. Because zip is lazy, the resulting iterator takes up hardly any memory. That means we can collect and move around massive amounts of data on our machine without holding it in memory.
The resulting single sequence is also the perfect target for mapping a function across because map takes a function and a sequence to which you want to apply that function. Because map is lazy as well, we can calculate all of these sales figures without much memory overhead. Indeed, all of our lazy functions, like map, filter, and zip, play nicely with one another. And because they all take sequences as inputs in one way or another, they can all be chained together and maintain their nice low-memory overhead laziness.