Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 634
Jack ma Avatar asked May 04 '26 16:05

Jack ma


1 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)
like image 63
blhsing Avatar answered May 06 '26 10:05

blhsing



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!