unpack the first two elements in list/tuple

后端 未结 5 1429
小蘑菇
小蘑菇 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条回答
  •  醉酒成梦
    2020-12-29 01:52

    On Python 3 you can do the following:

    >>> a, b, *_ = 1, 3, 4, 5
    >>> a
    1
    >>> b
    3
    

    _ is just a place holder for values you don't need

提交回复
热议问题