-2

I have 2 lists that are similar to this format:

a= [1,2,3]
b= [1,2,3,4,5,6]

I am looking to pair the lists in this particular format:

In [a,b] format:

[1,1]
[2,2]
[3,3]
[1,4]
[2,5]
[3,6]

I have read about numpy and itertools handling similar cases but I'm a little stuck in this case.

Thanks.

I'm not looking for itertools.izip_longest because I do not want None values. Everything must be paired as indicated in the example, above.

COCO
  • 148
  • 1
  • 2
  • 14

3 Answers3

4

Is this what you're looking for?

In [16]: a = [1, 2, 3]

In [17]: b = [1, 2, 3, 4, 5, 6]

In [18]: list(zip(itertools.cycle(a), b))
Out[18]: [(1, 1), (2, 2), (3, 3), (1, 4), (2, 5), (3, 6)]
Remi Guan
  • 21,506
  • 17
  • 64
  • 87
inspectorG4dget
  • 110,290
  • 27
  • 149
  • 241
  • This is EXACTLY what I'm looking for. Thank you for a concise yet very powerful solution. – COCO Nov 07 '15 at 02:01
1

Kinda ugly but anyways,

count = 0
output = []
while count != len(b):
    for i in range(len(a)):
        array = [a[i], b[count]]
        output.append(array)
        count += 1
Kamejoin
  • 347
  • 2
  • 8
1
[[a[i % len(a)], b[i]] for i in range(len(b))]
Tom Zych
  • 13,329
  • 9
  • 36
  • 53