How to convert a nested list into a one-dimensional list in Python?

compski picture compski · Jul 5, 2013 · Viewed 44.4k times · Source

I tried everything (in my knowledge) from splitting the array and joining them up together and even using itertools:

import itertools

def oneDArray(x):
    return list(itertools.chain(*x))

The result I want:

a) print oneDArray([1,[2,2,2],4]) == [1,2,2,2,4]

Strangely, it works for

b) print oneDArray([[1, 2, 3], [4, 5, 6], [7, 8, 9]]) == [1, 2, 3, 4, 5, 6, 7, 8, 9]

Question 1) How do I get part a to work the way I want (any hints?)

Question 2) Why does the following code above work for part b and not part a??

Answer

Ashwini Chaudhary picture Ashwini Chaudhary · Jul 5, 2013

You need to recursively loop over the list and check if an item is iterable(strings are iterable too, but skip them) or not.

itertools.chain will not work for [1,[2,2,2],4] because it requires all of it's items to be iterable, but 1 and 4 (integers) are not iterable. That's why it worked for the second one because it's a list of lists.

>>> from collections import Iterable
def flatten(lis):
     for item in lis:
         if isinstance(item, Iterable) and not isinstance(item, str):
             for x in flatten(item):
                 yield x
         else:        
             yield item

>>> lis = [1,[2,2,2],4]
>>> list(flatten(lis))
[1, 2, 2, 2, 4]
>>> list(flatten([[1, 2, 3], [4, 5, 6], [7, 8, 9]]))
[1, 2, 3, 4, 5, 6, 7, 8, 9]

Works for any level of nesting:

>>> a = [1,[2,2,[2]],4]
>>> list(flatten(a))
[1, 2, 2, 2, 4]

Unlike other solutions, this will work for strings as well:

>>> lis = [1,[2,2,2],"456"]
>>> list(flatten(lis))
[1, 2, 2, 2, '456']