1

I have a python list of lists that includes integers and I need to convert it into a single list.

If the list did not include any integers (only other lists) I could use the solution provided here: Making a flat list out of list of lists in Python

i.e.,

[item for sublist in main_list for item in sublist]

e.g,

test_list = [[1,2,3,4,5,6], [7,8,9,10], [11,12,13,14]]
test_list = [item for sublist in test_list for item in sublist]
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14]

However, this solution does not work if the list has integers in it:

test_list_2 = [[1,2,3,4,5,6], 0, [7,8,9,10], [11,12,13,14]]
test_list_2 = [item for sublist in test_list_2 for item in sublist]

Traceback (most recent call last):

File "<ipython-input-28-c6531e09f706>", line 1, in <module>
test_list_2 = [item for sublist in test_list_2 for item in sublist]

TypeError: 'int' object is not iterable

Is there a way to circumvent this problem? Thanks.

Community
  • 1
  • 1
Zlo
  • 1,150
  • 2
  • 18
  • 38
  • To me, it looks like an architectural problem. Couldn't you just add lists with a single element in the first place? – SBI Aug 24 '15 at 11:27
  • @SBI the sublists are added in a for loop for each analysis, the integers ('0s' in my case) are added if the `try` sequence failed and – Zlo Aug 24 '15 at 11:30
  • So the single digit appearing is always a zero? Could you just avoid adding the value if the try fails? Or instead of adding it directly, adding it to a list? – SBI Aug 24 '15 at 11:31
  • 2
    instead of adding `0` or whatever why not add `[0,]` or `[]` if the "try sequence failed" – scytale Aug 24 '15 at 11:34
  • 3
    http://stackoverflow.com/questions/2158395/flatten-an-irregular-list-of-lists-in-python – Padraic Cunningham Aug 24 '15 at 11:58

6 Answers6

4

You'd need to do something like this:

>>> test_list_2 = [[1,2,3,4,5,6], 0, [7,8,9,10], [11,12,13,14]]
>>> def lift_int(v):
...     if not isinstance(v, list): 
...         return [v]
...     else:
...         return v
... 
>>> test_list_2 = [item for sublist in test_list_2 for item in lift_int(sublist)]
>>> test_list_2
[1, 2, 3, 4, 5, 6, 0, 7, 8, 9, 10, 11, 12, 13, 14]
Markus Meskanen
  • 19,939
  • 18
  • 80
  • 119
Dan D.
  • 73,243
  • 15
  • 104
  • 123
3

Since you are trying to flatten your list I think the best way to do this is using generator function. Note that this flatten recursively your list not just one level.

>>> def flatten(items):
...     for item in items:
...         if isinstance(item, list):
...             yield from flatten(item)
...         else:
...             yield item
... 
>>> list(flatten(test_list_2))
[1, 2, 3, 4, 5, 6, 0, 7, 8, 9, 10, 11, 12, 13, 14]
styvane
  • 59,869
  • 19
  • 150
  • 156
0

If you wanted to continue with the list (in)comprehension, you could do this:

test_list = [[1,2,3,4,5,6], 0, [7,8,9,10], [11,12,13,14]]
print [item for sublist in test_list for item in (sublist if isinstance(sublist, list) else [sublist])]

test_list = [[1,2,3,4,5,6], 0, [7,8,9,10], 1, 2, 3, [11,12,13,14], 1234]
print [item for sublist in test_list for item in (sublist if isinstance(sublist, list) else [sublist])]

to make it even harder to comprehend.

mhawke
  • 84,695
  • 9
  • 117
  • 138
0

You can try this also

def convert_to_list(num):
    return [num] if not isinstance(num, list) else num

Then use itertools

>>>import itertools
itertools.chain(*map(convert_to_list, test_list_2))
<itertools.chain at 0x7ff211650550>
>>>list(itertools.chain(*map(convert_to_list, test_list_2)))
[1, 2, 3, 4, 5, 6, 0, 7, 8, 9, 10, 11, 12, 13, 14]
itzMEonTV
  • 19,851
  • 4
  • 39
  • 49
0
def convert(data):
    result = []
    for sublist in test_list_2:
        if type(sublist) is list:
            result += sublist
        else:
            result.append(sublist)
    return result

The convert function convert the list of lists and integers into a list

Hooting
  • 1,681
  • 11
  • 20
0

You could also simply use:

test_list = reduce(lambda x, y: x + (y if isinstance(y, list) else [y]), test_list)
klasske
  • 2,164
  • 1
  • 24
  • 31