python: iterate over a nested list and a flat list together

Viewed 33

I am trying to iterate over two lists (nested and a flat list) together:

eg:

x_list = ['11', '22']
y_list = [[33, 44], [55, 66, 77]]


for x, y in zip(x_list, y_list):
    print(x,y)

output:

11 [33, 44]
22 [55, 66, 77]

but I want to output like:

11 33

11 44

22 55

22 66

22 77

2 Answers

You can iterate over items in y in a nested loop:

for x, y in zip(x_list, y_list):
    for i in y:
        print(x, i)

You need to use two loops. You will use the first loop as it is, and a second loop to iterate over y, something like below:

for x, y in zip(x_list, y_list):
    for yi in y:
        print(x, yi)
Related