How to unpack a list of lists n-th number of times?

The point is that Ganga in the database have fields with value ArrayField(models.IntegerField). I filtered the data. Take pull this data using values_list.
Get data kinds:
[([123],), (None), (None), (None), (None), (None,), ([65, 66],), ([123],), [123]]

Need to get a list of numbers:
[123, 65, 66, 123, 123]
How to implement it?
April 4th 20 at 00:44
2 answers
April 4th 20 at 00:46
Solution
It was enough to do .values_list(flat=True)
Then get a list of lists.
And unpack it like so:
flat_list = [item for sublist in lst if sublist for item in sublist ]
April 4th 20 at 00:48
Solution
from itertools import chain

def flatten(seq):
try:
 yield from chain.from_iterable(flatten(x) for x in seq if x is not None)
 except TypeError:
 yield seq

flat_list = list(flatten(lst))
Instead of try/except you can use the hasattr(seq, '__iter__').

If you do not have to filter to None, then all it turns out simply:
def flatten(seq):
try:
 yield from chain(*map(flatten, seq))
 except TypeError:
 yield seq

Find more questions by tags Python