Nested for loop in Python to get equivalent result
Question:
I have two for loops to get the result as below.
First loop:
for row1 in value1:
print(row1)
The result is:
A
B
C
Second loop:
for row2 in value2:
print(row2)
The result is:
A
B
C
The result is expected as below using both the loops:
A A
B B
C C
I have tried using nested loops:
for row1 in value1:
for row2 in value2:
print(row1, row2)
The result is coming in as Cartesian values:
A A
A B
A C
B A
B B
B C
C A
C B
C V
What can I try next?
Answers:
value1=['A','B','C']
value2=['A','B','C']
for x,y in enumerate(value1):
print(value2[x],y)
A A
B B
C C
Or
for x,y in zip(value1,value2):
print(x,y)
A A
B B
C C
I have two for loops to get the result as below.
First loop:
for row1 in value1:
print(row1)
The result is:
A
B
C
Second loop:
for row2 in value2:
print(row2)
The result is:
A
B
C
The result is expected as below using both the loops:
A A
B B
C C
I have tried using nested loops:
for row1 in value1:
for row2 in value2:
print(row1, row2)
The result is coming in as Cartesian values:
A A
A B
A C
B A
B B
B C
C A
C B
C V
What can I try next?
value1=['A','B','C']
value2=['A','B','C']
for x,y in enumerate(value1):
print(value2[x],y)
A A
B B
C C
Or
for x,y in zip(value1,value2):
print(x,y)
A A
B B
C C