Python zip() Function with Examples

The zip() function in Python is a built-in function that allows you to combine two or more iterables into a single iterable object. It takes two or more iterables (e.g., lists, tuples, or strings) and returns a zip object that contains tuples, where the i-th tuple contains the i-th element from each of the input iterables. The length of the output zip object is determined by the length of the shortest input iterable.


Syntax:
zip(*iterables)

The zip() function can accept any number of iterables as arguments, separated by commas, and returns a zip object.

Combining two lists using zip()

numbers = [1, 2, 3, 4] colors = ["red", "blue", "green", "yellow"] zip_object = zip(numbers, colors) print(list(zip_object))
#Output: [(1, 'red'), (2, 'blue'), (3, 'green'), (4, 'yellow')]

In the above example, two lists numbers and colors. Pass these two lists to the zip() function to create a zip object. Then convert the zip object to a list using the list() function and print it. The output shows that each tuple contains an element from the numbers list and an element from the colors list.

Combining three lists using zip()

list1 = [1, 2, 3, 4] list2 = ["one", "two", "three", "four"] list3 = ["red", "blue", "green", "yellow"] zip_object = zip(list1, list2, list3) print(list(zip_object))
#Output: [(1, 'one', 'red'), (2, 'two', 'blue'), (3, 'three', 'green'), (4, 'four', 'yellow')]

In the above example, three lists list1, list2, and list3. Pass these three lists to the zip() function to create a zip object. Then convert the zip object to a list using the list() function and print it. The output shows that each tuple contains an element from the list1, an element from the list2, and an element from the list3.

Using zip() to unzip a list of tuples

zipped_list = [(1, 'red'), (2, 'blue'), (3, 'green'), (4, 'yellow')] numbers, colors = zip(*zipped_list) print(numbers) print(colors)
#Output: (1, 2, 3, 4) ('red', 'blue', 'green', 'yellow')

In the above example, a list of tuples zipped_list. Use the zip() function with the * operator to unzip the list of tuples into two separate lists numbers and colors. Then print the two lists. The output shows that the numbers list contains the first elements of each tuple, and the colors list contains the second elements of each tuple.

Following are some examples of using the zip() function with iterables other than lists.

Zip two Strings

string1 = 'hello' string2 = 'world' zipped = zip(string1, string2) for pair in zipped: print(pair)
#Output: ('h', 'w') ('e', 'o') ('l', 'r') ('l', 'l') ('o', 'd')

In the above example, create two strings string1 and string2 and use the zip() function to create an iterator that produces tuples containing the corresponding characters of the two strings. Then loop over the iterator and print each tuple.

Zip a range Object and a String

numbers = range(1, 7) string = 'Python' zipped = zip(numbers, string) for pair in zipped: print(pair)
#Output: (1, 'P') (2, 'y') (3, 't') (4, 'h') (5, 'o') (6, 'n')

In the above example, create a range object numbers containing the numbers 1 to 4, and a string string containing the letters 'Python'. Use the zip() function to create an iterator that produces tuples containing the corresponding elements of the two iterables. Then loop over the iterator and print each tuple.

Zip two tuples

tuple1 = (1, 2, 3) tuple2 = ('a', 'b', 'c') zipped = zip(tuple1, tuple2) for pair in zipped: print(pair)
#Output: (1, 'a') (2, 'b') (3, 'c')

In the above example, create two tuples tuple1 and tuple2 and use the zip() function to create an iterator that produces tuples containing the corresponding elements of the two tuples. Then loop over the iterator and print each tuple.

Zip two dictionaries with different keys

dict1 = {'a': 1, 'b': 2, 'c': 3} dict2 = {'x': 'red', 'y': 'blue', 'z': 'green'} zipped = zip(dict1, dict2) for pair in zipped: print(pair)
#Output: ('a', 'x') ('b', 'y') ('c', 'z')

In the above example, create two dictionaries dict1 and dict2 with different keys, and use the zip() function to create an iterator that produces tuples containing the corresponding values of the two dictionaries based on the keys of the first dictionary. Since the keys of dict2 do not match the keys of dict1, the values of dict2 are matched with the keys of dict1 in order.

Zip three dictionaries

dict1 = {'a': 1, 'b': 2, 'c': 3} dict2 = {'x': 'one', 'y': 'two', 'z': 'three'} dict3 = {'r': 'red', 'b': 'blue', 'g': 'green'} zipped = zip(dict1, dict2, dict3) for triple in zipped: print(triple)
#Output: ('a', 'x', 'r') ('b', 'y', 'b') ('c', 'z', 'g')

In this example, create three dictionaries dict1, dict2, and dict3 with the same keys, and use the zip() function to create an iterator that produces tuples containing the corresponding values of the three dictionaries. Then loop over the iterator and print each tuple.

What are the practical uses of Python Zip?

The zip() function in Python is a versatile tool for working with iterables. Here are some practical use cases for the zip() function:

  1. Pairing items in two or more lists - You can use the zip() function to pair items in two or more lists together based on their position in the list. This can be useful when you want to perform operations on corresponding elements of two or more lists, such as adding or subtracting them.
  2. Iterating over multiple lists simultaneously - The zip() function can be used to iterate over multiple lists simultaneously, producing tuples containing the corresponding elements of each list. This can simplify the code and make it more readable, compared to using nested loops to iterate over each list.
  3. Merging dictionaries - You can use the zip() function to merge two dictionaries into a single dictionary, where the keys of each dictionary become the keys of the resulting dictionary, and the values of each dictionary become the values of the resulting dictionary.
  4. Transposing data - If you have a list of lists, you can use the zip() function to transpose the data, meaning that you can swap the rows and columns of the data. This can be useful when you want to perform operations on columns of data, instead of rows.
  5. Creating enumerated lists - You can use the zip() function with the range() function to create enumerated lists, where each item in the list is paired with a corresponding index number.

Conclusion:

The zip() function is a useful tool for manipulating and iterating over iterables in Python. Its versatility makes it a valuable tool in a variety of use cases.