Zipping unequal lists in python in to a list which does not drop any element from longer list being zipped

I have two lists

  a = [1,2,3]
  b = [9,10]

I want to combine (zip) these two lists into one list c such that

  c = [(1,9), (2,10), (3, )]

Is there any function in standard library in python to do this.

Python List comprehension to create unequal length lists from a list using conditional

Using list comprehension, itertools or similar functions, is it possible to create two unequal lists from a list based on a conditional? Here is an example: main_list = [6, 3, 4, 0, 9, 1] part_list =

Zipping lists within a list in Python

I have a list of lists big_list = [[‘a1′,’b1′,’c1’], [‘a2′,’b2′,’c3’], [‘a3′,’b3′,’c3’]] how do I zip the lists within this list? what I want to do is zip(list1,list2,list3), but do this dynamically

Create lists with any two elements from a longer list DrRacket

How can I generate serial lists by combining any two elements from a longer list, say with 4 elements? For example, I want to get ‘(1 2), ‘(1 3), ‘(1 4), ‘(2 3), ‘(2 4), and ‘(3 4) based on ‘(1 2 3 4)

Scheme List of lists zipping

If my input is a list of lists, then I want to output a list with elements from the input so that they are shuffled like a deck of playing cards. For example, if input is ‘((1 2 3) (4 5)) then I want

Find Largest Element in 2 Ordered Lists which does not occur in one list

Hi I’ve searched here but can’t find an answer to my problem. I’m using Python and have 2 lists. They are both ordered. The first list is generally the longer one (approx 10,000 elements) and it never

jQuery Drag from a List (without removing element) and Drop into another List

How can I have two lists (div or table elements) in which I can drag & drop items from one to the other (I think jQuery droppable does this), but without actually removing it from the source list?

LISP – removing an element from a list with nested lists

I need to remove an element from a list which contain inner lists inside. The predefined element should be removed from every inner list too. I have started working with the following code: (SETQ L2

Zipping nested lists in Python

Say I have the following two lists/numpy arrays: List1 = [[1,2,3,4], [10,11,12], …] List2 = [[-1,-2-3,-4], [-10,-11,-12], …] I would like to obtain a list that holds the zipping of the nested lis

Zipping list of lists of lists lose decimals

I have a list of lists of lists: [ [[‘1’, ‘3.61912’, ‘3.25558’, ‘3.98787’], [‘2’, ‘5.35465’, ‘4.98407’, ‘5.82305’], [‘3’, ‘14.8184’, ‘13.8878’, ‘15.7686’], [‘4’, ‘0.278589’, ‘0.25531’, ‘0.302837’], [‘

Zipping unequal length lists in scala

I want something like this: def unequalZip[A, B](a: Iterable[A], b: Iterable[B]) = Iterable[(Option[A], Option[B])] where the items from the shorter iterable is matched with items from longer iterable


What you seek is itertools.izip_longest

>>> a = [1,2,3]
>>> b = [9,10]
>>> for i in itertools.izip_longest(a,b): print i
(1, 9)
(2, 10)
(3, None)

EDIT 1: If you really want to get rid of the Nones, then you could try:

>>> for i in (filter(None, pair) for pair in itertools.izip_longest(a,b)): print i
(1, 9)
(2, 10)

EDIT 2: In response to steveha’s comment:

filter(lambda p: p is not None, pair) for pair in itertools.izip_longest(a,b)

Another way is map:

a = [1, 2, 3]
b = [9, 10]
c = map(None, a, b)

Although that will too contain (3, None) instead of (3,). To do that, here’s a fun line:

c = (tuple(y for y in x if y is not None) for x in map(None, a, b))

It’s not too hard to just write the explicit Python to do the desired operation:

def izip_short(a, b):
    ia = iter(a)
    ib = iter(b)
    for x in ia:
            y = next(ib)
            yield (x, y)
        except StopIteration:
            yield (x,)
    for x in ia:
        yield (x,)
    for y in ib:
        yield (None, y)

a = [1, 2, 3]
b = [9, 10]
list(izip_short(a, b))
list(izip_short(b, a))

I wasn’t sure how you would want to handle the b sequence being longer than the a sequence, so I just stuff in a None for the first value in the tuple in that case.

Get an explicit iterator for each sequence. Run the a iterator as a for loop, while manually using next(ib) to get the next value from the b sequence. If we get a StopIteration on the b sequence, we break the loop and then for x in ia: gets the rest of the a sequence; after that for y in ib: will do nothing because that iterator is already exhausted. Alternatively, if the first for x in ia: loop exhausts the a iterator, the second for x in ia: does nothing but there could be values left in the b sequence and the for y in ib: loop collects them.

c = zip(a, b) + a[len(b):] + b[len(a):]