3

I use list comprehension in such a way that for each element I have 2 resulting values:

my_list = [10,20,30]
res_list = [ (x*2, x*3) for x in my_list ]
res_list # [(20, 30), (40, 60), (60, 90)]

But I need to have a flatten list, so I have to perform another comprehension:

res_list_1 = [yy for xx in res_list for yy in xx]
res_list_1 # [20, 30, 40, 60, 60, 90]

Is there any way to avoid this and get flat res_list directly on the first comprehension?

Vasily A
  • 8,256
  • 10
  • 42
  • 76

1 Answers1

6

Put the tuple at the end:

res_list_1 = [item for object in my_list for item in (object*2, object*3)]
TigerhawkT3
  • 48,464
  • 6
  • 60
  • 97