Head and tail in one line

后端 未结 5 634
独厮守ぢ
独厮守ぢ 2020-11-29 00:32

Is there a pythonic way to unpack a list in the first element and the \"tail\" in a single command?

For example:

>> head, tail = **some_magic a         


        
5条回答
  •  星月不相逢
    2020-11-29 01:04

    >>> mylist = [1, 1, 2, 3, 5, 8, 13, 21, 34, 55]
    >>> head, tail = mylist[0], mylist[1:]
    >>> head
    1
    >>> tail
    [1, 2, 3, 5, 8, 13, 21, 34, 55]
    

提交回复
热议问题