In this post, I will explain you how to zip two list together in python and returns the merge list. These two lists can be equal or unequal length.
Python provides zip() function
for merging the two lists.
The purpose of zip() function is to map a similar index of multiple containers so that they can be used just as a single entity.
Lets understand how to zip two lists in python with different examples.
Example 1- How to zip two lists of Equal lengths and Same Datatype in Python
Let’s understand this example with below python code
#Define the list x and list y x = [1,2,3] y = [4,5,6] #zip the two lists together into one list result = list(zip(x, y)) print("The result after applying zip function : ",result)
In the above example, we had created x and y lists of same length and same datatype (integer)
Python zip()
function which takes x and y list as the input value. It zip two lists together. Python zip list return merged list.
//Output The result after applying zip function : [(1, 4), (2, 5), (3, 6)]
Above code gives us the new merged list as [(1, 4), (2, 5), (3, 6)].So now we will be able to use this new list as a single list with 3 elements as shown.
Example 2- How to zip two lists of Equal lengths but Different Datatype in Python
In this example we have two list one of alphabetic characters and second of numbers. Although it zips the two lists of different datatype together in the similar manner it does with same datatype.
#Define the list x and list y x = ['a','b','c'] y = [1,2,3] #zip the two lists together into one list result = list(zip(x, y)) print("The result after applying zip function : ",result)
Python zip()
function which takes x and y list as the input value. It zip two lists together. Python zip list returns the new merged list.
Above code gives us the new merged list as [(‘a’, 1), (‘b’, 2), (‘c’, 3)]. We will be able to use this new list as a single list with 3 elements as shown below.
//Output The result after applying zip function : [('a', 1), ('b', 2), ('c', 3)]
Example 3- How to zip two unequal list of lengths
Let’s assume we have two lists x and y of unequal length as below:-
x = [‘a’,’b’,’c’,’d’,’e’]
y = [1, 2, 3,4]
Lets apply the zip function on these two unequal lists.
#Define the list x and list y x = ['a','b','c','d','e'] y = [1,2,3,4] #zip the two lists together into one list result = list(zip(x, y)) print("The result after applying zip function : ",result)
Zip() function will zip two unequal lists together and python zip list returns the shortest length list.
Here length of x is 5 and y is 4. So y has shortest length.
That’s why resultant list is of length 4.The output of the above code is mentioned below.
//Output The result after applying zip function : [('a', 1), ('b', 2), ('c', 3), ('d', 4)]
Example 4- How to zip two lists of Equal lengths into Dictionary in Python
Let’s assume we have keys and values lists as below
keys = [‘x’, ‘y’, ‘z’]
values = [1, 2, 3]
Zip them into a dictionary with below python code.
#define list of keys and list of values keys = ['x', 'y', 'z'] values = [1, 2, 3] #zip the two lists together into one dictionary result = dict(zip(keys, values)) print("The new Created Dictionary is: ",result)
In the above code, zip() function takes the keys and values as input parameters and results the single list .By using dict() function we converted the resultant list into dictionary.
So final result gives us the dictionary with key,value pair.The output of the above code is mentioned below.
//Output The new Created Dictionary is: {'x': 1, 'y': 2, 'z': 3}
Conclusion
I hope, you may find tutorial on how to zip two lists together in python educational and helpful.