This program will produce the following result: Print (" Unzipped list of capitals : ", capitals) Print (" Unzipped list of countries : ", countries) Zip_result = zip(list_of_countries, list_of_capitals)
# Call the zip function on test input lists Print (" Input List (2) : " + str(list_of_capitals))
# Python zip function with asterisk (*) in argument Given below is an example to help you understand it better. The difference is an asterisk sign that you will need to prepend to the zip argument. And interestingly, you’ll be doing it through the zip() function only. You can also unzip the Python zip object or extract the output of the zip() function.
This example will give the following outcome: Print (" Zipped resultant list is : \n", result_set) Print (" Size of the zipped resultant list is : ", len(result_set)) Print ("\nTest Result: **********\n Size of the smallest list is : ", size_smallest_list) # Call the zip function on input lists of different sizes Size_smallest_list = min(len(in_list1), len(in_list2), len(in_list3)) Print (" Input List (3) : " + str(in_list3)) Print (" Input List (2) : " + str(in_list2)) # Zip two or more lists of different sizes And it is the one to decide the length of the result of the Python zip() function. Therefore, the third list, which has four elements, is the smallest. In the below example, we’ve used three lists for the demo with their respective sizes 5, 6, and 4. Let’s now try to zip multiple lists (more than two), and see how the zip function pairs their elements together. Python zip example with iterables of varied sizes Let’s now summarize the execution output:
The above sample used Python type() to confirm the same. You may check from the result that zip function generates a Python zip object. Print (" The final result after zip() : \n", final_res) Print ("\nTest Result: **********\n Type of zip_obj is : ", type(zip_obj)) Zip_obj = zip(prog_langs, tiobe_ranks, features) Print (" TIOBE Ranks : " + str(tiobe_ranks)) Simple zip() function example # Python zip function simple example N is the length of the shortest of iterables in the zip() parameters. In the case of multiple iterable arguments, the values at a similar index are grouped to form a tuple, and this process continues N times.It means that every tuple will have one element. When only one iterable is passed, zip() still returns an iterator holding tuples with a single cardinal value.of arguments, zip() returns a zero-sized iterator. The zip() function returns a consolidated iterator that contains tuples holding adjacent values from input containers. Python iterables or collections such as a list, string, dictionary, set, or any custom iterables. )Īlternatively, the following syntax can also be referred: ''' Syntax(2) ''' The size of the zip object depends on the shortest of the iterables passed to the Python zip function. It is used to create an iterator of tuples (known as zip object) from a list of iterables passed as arguments.Įach tuple in the iterator contains elements that exist at a similar index in all the input iterables. 5 Zip and for loop to iterate over two lists in parallel.
3 Python zip example with iterables of varied sizes.