In Python
zip()
Functions are a very useful built-in function to aggregate multiple iterable objects (such as lists, tuples, dictionaries, etc.) into a tuple and return objects composed of these tuples.
1. zip() usage
1.1 Basic usage
zip()
Functions are used to combine elements in multiple iterable objects into tuples by position. Here is a basic example:
a = [1,2,3] b = ["a", "b", "c"] c = zip(a, b) for i in c: print(i)
Code output:
(1, 'a')
(2, 'b')
(3, 'c')
1.2 Iterable objects of different lengths
If the input iterable object length is different,zip()
The compress will be performed based on the shortest iterable object, and the excess elements will be discarded:
list1 = [1, 2, 3] list2 = ('a', 'b') result = zip(list1, list2) print(list(result))
Code output:
[(1, 'a'), (2, 'b')]
As you can see, 3 in list1 is discarded because list2 has a length of 2.
1.3 Multiple iterable objects
zip()
Supports multiple iterable objects. They can be combined in order
list1 = [1, 2, 3] list2 = ['a', 'b', 'c'] list3 = [True, False, True] result = zip(list1, list2, list3) print(list(result))
Code output:
[(1, 'a', True), (2, 'b', False), (3, 'c', True)]
1.4 zip and dictionary
If the iterable object passed in is a dictionary,zip()
The keys will be pressed to pair. Therefore, it is usually combineddict()
Use to create a dictionary.
keys = ['name', 'age', 'city'] values = ['Alice', 25, 'New York'] result = zip(keys, values) dictionary = dict(result) print(dictionary)
Code output:
{'name': 'Alice', 'age': 25, 'city': 'New York'}
1.5 Unzip zip
zip()
The result generated is an iterator, and we can use iterator for already compressed datazip(*iterables)
To perform the decompression operation
list1 = [1, 2, 3] list2 = ('a', 'b', 'c') result = zip(list1, list2) # Uncompressionunpacked = zip(*result) print(list(unpacked))
Code output:
[(1, 2, 3), ('a', 'b', 'c')]
2. Zip() application scenario
2.1 Traversing multiple sequences in parallel
zip()
Functions are often used to traverse multiple sequences in parallel, and to take out the positions of corresponding elements in each sequence one by one in a loop.
names = ['Alice', 'Bob', 'Charlie'] ages = [25, 30, 35] for name, age in zip(names, ages): print(f"{name} is {age} years old.")
2.2 Building a dictionary
As mentioned earlier,zip()
anddict()
Use it in combination to create dictionaries very conveniently
keys = ['name', 'age', 'city'] values = ['Alice', 25, 'New York'] result = dict(zip(keys, values)) print(result)
2.3 for matrix transposition
In some cases,zip()
It can be used for matrix transposition and other operations. For example, merge multiple rows into columns, or merge multiple columns into rows:
matrix = [(1, 2, 3), (4, 5, 6), (7, 8, 9)] transposed = zip(*matrix) print(list(transposed))
Code output:
[(1, 4, 7), (2, 5, 8), (3, 6, 9)]
Summarize
This is the article about the detailed explanation of the usage and application scenarios of zip() function in Python. For more detailed explanation of Python zip() function, please search for my previous article or continue browsing the related articles below. I hope everyone will support me in the future!