unpack the first two elements in list/tuple

后端 未结 5 1430
小蘑菇
小蘑菇 2020-12-29 01:30

Is there a way in python to do like this:

a, b, = 1, 3, 4, 5

And then:

>>> a
1
>>> b
3

5条回答
  •  猫巷女王i
    2020-12-29 01:56

    There is no way to do it with the literals that you've shown. But you can slice to get the effect you want:

    a, b = [1, 3, 4, 5, 6][:2]
    

    To get the first two values of a list:

    a, b = my_list[:2]
    

提交回复
热议问题