0

I have a list like:

l = [[1,2,3],[4,5]]

I would like to unpack each element to make it like:

l = [1,2,3,4,5]

I have my solution here:

l = reduce(lambda x, y: x+y, l)

Anyone has other Pythonic way? Thanks.

user2963623
  • 2,267
  • 1
  • 14
  • 25
timchen
  • 378
  • 3
  • 13
  • 1
    Note: your solution is quadratic in time. You shouldn't use it not because it isn't readable, but because it is asymptotically worse. – Bakuriu Jun 30 '14 at 09:43

1 Answers1

0

You should use itertools methods;

from itertools import chain

l = [[1,2,3],[4,5]]
list(chain.from_iterable(l))
myildirim
  • 2,248
  • 2
  • 19
  • 25
  • You're welcome, if this answer is correct you, should close the question; set this answer as your accepted answer. @timchen – myildirim Jun 30 '14 at 09:43