Python zip() Built in Function

The zip() function in Python is a built-in function that returns a zip object, which is an iterator of tuples where the first item in each passed iterator is paired together, and then the second item in each passed iterator are paired together etc.

Here are some examples of using the zip() function:

# Example 1: Zipping two lists of equal length
names = ['apple', 'banana', 'cherry']
count = [25, 31, 85]
result = zip(names, count)
for item in result:
    print(item)
# Output: ('apple', 25), ('banana', 31), ('cherry', 85)

# Example 2: Zipping two lists of different length
index = [1, 2, 3]
languages = ['python', 'c', 'c++', 'java']
dictionary = dict(zip(index, languages))
print(dictionary)
# Output: {1: 'python', 2: 'c', 3: 'c++'}

If you have any questions about this code, you can drop a line in comment.

Comments

Popular posts from this blog

Python chr() Built in Function

Stock Market Predictions with LSTM in Python

Collections In Python

Python Count Occurrence Of Elements

Python One Liner Functions