-2

I have two lists:

list1 = [1, 2, 1, 2, 3, 2, 4, 1]
list2 = [a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p]

I want to create a new list of tuples that will look like the one below:

result = [ 
           (a,),
           (b, c),
           (d,),
           (e, f),
           (g, h, i),
           (j, k),
           (l, m, n, o),
           (p,)   
          ]
3
  • 1
    Welcome to Stack Overflow! It looks like you want us to write some code for you. While many users are willing to produce code for a coder in distress, they usually only help when the poster has already tried to solve the problem on their own. A good way to demonstrate this effort is to include the code you've written so far, example input (if there is any), the expected output, and the output you actually get (console output, stack traces, compiler errors - whatever is applicable). The more detail you provide, the more answers you are likely to receive.
    – Martijn Pieters
    Feb 24, 2015 at 11:37
  • What if the list1 is [1, 2, 1, 2, 3, 2, 4, 4] ? Feb 24, 2015 at 11:38
  • sum(list1) is always equal to len(list2)
    – rem
    Feb 24, 2015 at 11:44

2 Answers 2

1

You could do this in a generator:

def divvy_up(lst, lengths):
    pos = 0
    for length in lengths:
        yield tuple(lst[pos:pos + length])
        pos += length

which will produce tuples taken from lst for each length specified in lengths:

>>> import string
>>> list1 = [1, 2, 1, 2, 3, 2, 4, 1]
>>> list2 = list(string.ascii_lowercase[:16])
>>> list2
['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p']
>>> list(divvy_up(list2, list1))
[('a',), ('b', 'c'), ('d',), ('e', 'f'), ('g', 'h', 'i'), ('j', 'k'), ('l', 'm', 'n', 'o'), ('p',)]

To extend this to any iterable (rather than sequences like lists), you can use itertools.islice() here:

from itertools.islice

def divvy_up(it, lengths):
    it = iter(it)
    for length in lengths:
        yield tuple(islice(it, length))
0
0
cumsum = [sum(list1[0:i]) for i in range(1, len(list1) + 1)]
[tuple(list2[a:b]) for a, b in zip([None] + cumsum[0:-1], cumsum)]

Gives exactly that answer

2
  • That's ok on the example data, but it's a bit wasteful if list1 has a lot of elements. It would be better to produce cumsum in a normal for loop instead of a list comprehension. Sure, the list comp can be done in one line, but the equivalent simple for loop is generally faster. And in this case it'd be a lot faster since it doesn't need to recalculate the sum from the beginning of the source list each time.
    – PM 2Ring
    Feb 24, 2015 at 13:02
  • # PM 2Ring, Well. You're right, it's only for small lists. Also if it was python3.2+ we'd just use itertools.accumulate. I'm sure it has required performance . Or for bigger lists we can use numpy.accumulate Feb 24, 2015 at 13:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.