A = [1,3,5]
B = ['a','b']
for x in A , for y in B :
print(x,y)
how we can implement two for loop and can convert 2D list in 1D list
desired output : [(1,'a') ,(2,'b') ,(3,'a') ,(3,'b') ,(5,'a'),(5,'b') ]
A = [1,3,5]
B = ['a','b']
for x in A , for y in B :
print(x,y)
how we can implement two for loop and can convert 2D list in 1D list
desired output : [(1,'a') ,(2,'b') ,(3,'a') ,(3,'b') ,(5,'a'),(5,'b') ]
If you just want to iterate through two lists simultaneously you can use zip:
x = [1, 3, 5]
y = ['a', 'b', 'c']
for i, j in zip(x, y):
print(i, j) # Will print "1 a" followed by "3 b"
Note that zip will actually return a list of tuples containing the combined elements from both lists, and if the lists are not equal in length then the longer list will just be truncated
If you want equal distribution of elements in both lists:
A = [1,3,5]
B = ['a','b']
l = []
for x in A:
for y in B:
l.extend([x, y])
>>> print(l)
[1, 'a', 1, 'b', 3, 'a', 3, 'b', 5, 'a', 5, 'b']
A = [1,3,5]
B = ['a','b']
l = []
#for each element in A
for i in A:
#for each element in B
for o in B:
l.append((i,o))
Iterate through each element in each list then append them together