Ibrahim Mohammed
Ibrahim Mohammed

Reputation: 27

How do I merge values from a list to a list of lists

I have a a list and a list of lists that look like this

groups = ['1','2','3','4','5']

weeks = [['e','e','e','e','e','e','e'],
       ['o','o','m','m','m','o','o'],
       ['l','l','l','l','l','o','o'],
       ['m','m','m','m','m','o','o'],
       ['m', 'm','m','m','m','o','o']]

I am able to iterate over the weeks for loop using this code

for i in range(5):
    for j in range(7):
        roster = (weeks[i][j])
        display(roster)

But I would like my output to be,

e 1
e 1
e 1 ... #for the first row in the weeks list then

o 2 
o 2
m 2 ... #for the next row of the  and so on till it reaches the last row.

Upvotes: 0

Views: 29

Answers (1)

blhsing
blhsing

Reputation: 106901

You can pair the items in groups with the sub-lists in weeks with zip:

for w, g in zip(weeks, groups):
    for i in w:
        print(i, g)

This outputs:

e 1
e 1
e 1
e 1
e 1
e 1
e 1
o 2
o 2
m 2
m 2
m 2
o 2
o 2
l 3
l 3
l 3
l 3
l 3
o 3
o 3
m 4
m 4
m 4
m 4
m 4
o 4
o 4
m 5
m 5
m 5
m 5
m 5
o 5
o 5

Upvotes: 1

Related Questions