Home > front end >  How to print a 1x2 list from 2 separate lists?
How to print a 1x2 list from 2 separate lists?

Time:06-30

I need to print 2 entries from list 2 for every one entry in list 1

So say

ll = "1,2,3,4,5,6,7,8,9"
l2 = "a,b,c,d,e,f,g,h,i"

I need to print

1 a b
2 c d
3 d e
4 f g
5 h i

and so on

I can do

for i,j in zip(matches,matches2):
    print (i,j)

but this just prints

1 a
2 b
3 c
4 d

but i can't work out how to print 2 entries from matches2 for every 1 entry in matches

CodePudding user response:

You can create an iterator from matches2 so that it can be extracted twice for each iteration within zip:

iter2 = iter(matches2)

for i, j, k in zip(matches, iter2, iter2):
    print(i, j, k)

CodePudding user response:

Using your first example strings you can do something like this:

ll = "1,2,3,4,5,6,7,8,9".split(',')
l2 = "a,b,c,d,e,f,g,h,i".split(',')

for i in range(0,len(ll)):
    j=i*2
    if j 1<len(l2):
        print(ll[i],l2[j],l2[j 1])
  • Related