Possible to extend two lists at once?

后端 未结 1 717
野的像风
野的像风 2021-01-29 16:43

I have two lists (list_1,list_2) and a function which returns two lists (list_1_f, list_2_f) and I would like to add the items of list_1_f to list_1 and the items of list_2_f to

相关标签:
1条回答
  • 2021-01-29 17:37

    It is not directly possible, because what must be on the left side of an assignment cannot be a function call. It can only be built from simple variables, data members, subscripts and commas, parentheses or square brackets.

    Best that can be done is to use a comprehension or a map on the right side:

    list_1, list_2 = map(lambda x: sum(x, []), zip((list_1, list_2), lists()))
    

    (thanks to @meowgoesthedog for that way)

    Whether it is better that a clear code using 3 lines is up to the reader. IMHO the only real use case would be inside a lambda which only supports a unique expression

    0 讨论(0)
提交回复
热议问题