0

Need help with this list comprehension...

Here is my code:

w1_comb_l1=[n1, pos_pred_list[0], [i for i in pos_days_w1]]

That comprehension gives this output: [209, 49684, [9351, 9346, 8103, 7676, 8739, 3925, 2544]].

I need help getting those values that are in inner brackets out, therefore, here is desired output: [209, 49684, 9351, 9346, 8103, 7676, 8739, 3925, 2544] - without inner list

It is probably a small fix.

martineau
  • 119,623
  • 25
  • 170
  • 301
DGomonov
  • 715
  • 1
  • 7
  • 19
  • 1
    In this particular case `[n1, pos_pred_list[0], *pos_days_w1]` should be enough. – bereal Jan 24 '20 at 18:46
  • @Austin I tried `w1_comb_l1=[n1, pos_pred_list[0], lambda l: [item for pos_days_w1 in l for item in pos_days_w1]]` it gives me **[209, 49684, (l)>]**, as you can see i'm storing the function in this case... – DGomonov Jan 24 '20 at 18:49
  • @bereal It does work! Any chance you could shine some light for me on how `*pos_days_w1` works or point to right reading? Thank you. – DGomonov Jan 24 '20 at 18:51
  • 1
    You can do `[y for x in w1_comb_l1 for y in x]`. But this is after creation of `w1_comb_l1`, which you can avoid using the trick from @bereal. – Austin Jan 24 '20 at 18:54
  • 1
    @DGomonov this is a feature called [unpacking](https://docs.python.org/3/whatsnew/3.5.html#whatsnew-pep-448), in this generalized form it was introduced in Python 3.5. – bereal Jan 24 '20 at 18:55
  • `w1_comb_l1=[n1, pos_pred_list[0],*pos_days_w1]` – eatmeimadanish Jan 24 '20 at 19:12

0 Answers0