unpack the first two elements in list/tuple

后端 未结 5 1416
小蘑菇
小蘑菇 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:59

    Just to add to Nolen's answer, in Python 3, you can also unpack the rest, like this:

    >>> a, b, *rest = 1, 2, 3, 4, 5, 6, 7
    >>> a
    1
    >>> rest
    [3, 4, 5, 6, 7]
    

    Unfortunately, this does not work in Python 2 though.

提交回复
热议问题