The zip() function is a great tool to process multiple sequences during the same loop. As it looks at each sequence, zip() joins the individual items from the sequences and outputs tuples with parallel items from each sequence.
In other words, if you stack two rows of items on top of each other, zip() will gather the first two items from each row and combine them in a tuple. Then, it gets the second two items from each row and makes another tuple. It continues doing this until each row is finished. Following screenshot demonstrates this functionality:
Each element in each tuple is combined concurrently as the list is generated, as zip() simply grabs the item in each tuple that has the same index value, and then makes a new tuple from them.
Any type of iterable sequence can be used by zip(), even files (as they are just streams of characters). Also, it will only process the minimum number of items from all given sequences; if you have a list with three items and another with seven, you'll only get three tuples as the result.
The zip() function is great for combining data gathered at runtime, such as user input or from a file. If you need to make a dictionary after the program has been started, because you don't know what the actual dictionary values will be, zip() can help create that dictionary. While following screenshot demonstrates this, the keys and values are frequently from sources created after the program was started:
Line 7 shows how the Python built-in constructor dict() can be used to make an object creation request. It essentially makes a dictionary out of a list, and can be quite powerful when working with dictionaries. There are other constructors within Python, such as lists, and they are often used to programmatically create these objects during runtime.