Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to zip two lists with different lengths [duplicate]

Tags:

python

list

I want to zip two list with different length

for example

A = [1,2,3,4,5,6,7,8,9]
B = ["A","B","C"]

and I expect this

[(1, 'A'), (2, 'B'), (3, 'C'), (4, 'A'), (5, 'B'), (6, 'C'), (7, 'A'), (8, 'B'), (9, 'C')]

But the built-in zip won't repeat to pair with the list with larger size. Does there exist any built-in way can achieve this?

Here is my code:

idx = 0
zip_list = []
for value in larger:
    zip_list.append((value,smaller[idx]))
    idx += 1
    if idx == len(smaller):
        idx = 0
like image 662
user2131116 Avatar asked Oct 30 '13 15:10

user2131116


People also ask

Can you zip two lists of different lengths?

The zip() function will only iterate over the smallest list passed. If given lists of different lengths, the resulting combination will only be as long as the smallest list passed.

How to zip more than 2 lists Python?

The Python zip() function makes it easy to also zip more than two lists. This works exactly like you'd expect, meaning you just only need to pass in the lists as different arguments. What is this? Here you have learned how to zip three (or more) lists in Python, using the built-in zip() function!

Can you zip three lists Python?

Python zip three lists Python zipping of three lists by using the zip() function with as many inputs iterables required. The length of the resulting tuples will always equal the number of iterables you pass as arguments. This is how we can zip three lists in Python.


Video Answer


5 Answers

You can use itertools.cycle:

Make an iterator returning elements from the iterable and saving a copy of each. When the iterable is exhausted, return elements from the saved copy. Repeats indefinitely.

Example:

A = [1,2,3,4,5,6,7,8,9]
B = ["A","B","C"]

from itertools import cycle
zip_list = zip(A, cycle(B)) if len(A) > len(B) else zip(cycle(A), B)
like image 176
sloth Avatar answered Oct 18 '22 17:10

sloth


Try this.

A = [1,2,3,4,5,6,7,8,9]
B = ["A","B","C"]
Z = []
for i, a in enumerate(A):
    Z.append((a, B[i % len(B)]))

Just make sure that the larger list is in A.

like image 29
Eser Aygün Avatar answered Oct 18 '22 18:10

Eser Aygün


Solution for an arbitrary number of iterables, and you don't know which one is longest (also allowing a default for any empty iterables):

from itertools import cycle, zip_longest

def zip_cycle(*iterables, empty_default=None):
    cycles = [cycle(i) for i in iterables]
    for _ in zip_longest(*iterables):
        yield tuple(next(i, empty_default) for i in cycles)

for i in zip_cycle(range(2), range(5), ['a', 'b', 'c'], []):
    print(i)

Outputs:

(0, 0, 'a', None)
(1, 1, 'b', None)
(0, 2, 'c', None)
(1, 3, 'a', None)
(0, 4, 'b', None)
like image 33
Dane White Avatar answered Oct 18 '22 18:10

Dane White


Do you know that the second list is shorter?

import itertools
list(zip(my_list, itertools.cycle(another_list)))

This will actually give you a list of tuples rather than a list of lists. I hope that's okay.

like image 44
Frank Yellin Avatar answered Oct 18 '22 19:10

Frank Yellin


You can use itertools.cycle:

from itertools import cycle

my_list = [1, 2, 3, 5, 5, 9]
another_list = ['Yes', 'No']

cyc = cycle(another_list)

print([[i, next(cyc)] for i in my_list])
# [[1, 'Yes'], [2, 'No'], [3, 'Yes'], [5, 'No'], [5, 'Yes'], [9, 'No']]
like image 27
Henry Yik Avatar answered Oct 18 '22 18:10

Henry Yik