# Create two sample lists x = [1, 2, 3] y = [10, 20, 30]
# Use zip to get item pairs merged = zip(x, y)
# Print out the merged list print(list(merged))
[(1, 10), (2, 20), (3, 30)]
The Python zip function gets two lists as output. Then, the function starts extracting one item from each list. The pairs of items will be inserted in tuples and the tuples. Therefore, the output is a list of tuples.
Note: For zip to work, the two input lists should have the same number of items.
Learn Flask development and learn to build cool apps with our premium Python course on Udemy.
We will keep you Python-motivated if you subscribe below.