What will be the output of the following Python code?A = [[1, 2, 3], [4, 5, 6], [7, 8, 9]] B = [[3, 3, 3], [4, 4, 4], [5, 5, 5]] zip(A, B)
Question
What will be the output of the following Python code?
A = [[1, 2, 3],
[4, 5, 6],
[7, 8, 9]]
B = [[3, 3, 3],
[4, 4, 4],
[5, 5, 5]]
zip(A, B)
Solution
The zip()
function in Python returns 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.
If the passed iterators have different lengths, the iterator with the least items decides the length of the new iterator.
So, the output of the code will be an iterator that generates the following tuples:
- The first tuple will contain the first elements of A and B, which are [1, 2, 3] and [3, 3, 3] respectively.
- The second tuple will contain the second elements of A and B, which are [4, 5, 6] and [4, 4, 4] respectively.
- The third tuple will contain the third elements of A and B, which are [7, 8, 9] and [5, 5, 5] respectively.
So, the output will be:
<zip object at 0x7f4c6cd4dc80>
If you want to print the tuples, you can convert the zip object to a list:
print(list(zip(A, B)))
And the output will be:
[([1, 2, 3], [3, 3, 3]), ([4, 5, 6], [4, 4, 4]), ([7, 8, 9], [5, 5, 5])]
Similar Questions
What is the output of below code snippeta = [ [8, 2] , [4, 1, 2, 2] ]b = a[1][2:]print(b)
What will be the output of the following Python code snippet?a=[1, 4, 3, 5, 2]b=[3, 1, 5, 2, 4]a==bset(a)==set(b)
What will be the output of the given code?lis_1= [1,2,3]lis_2= [4,5,6]lis_3=[7,8,9]lis_1.append(lis_3)lis_2.extend(lis_3)print(lis_1,lis_2)
What will the output be of the following code?D = {1:['Raj', 22], 2:['Simran', 21], 3:['Rahul', 40]}for val in D: print(val)
What will be the output of the following Python code?l=[[1, 2, 3], [4, 5, 6]]for i in range(len(l)): for j in range(len(l[i])): l[i][j]+=10print(l)
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.