Add entry to beginning of list and remove the last one

后端 未结 5 2021
生来不讨喜
生来不讨喜 2020-12-16 16:40

I have a list of about 40 entries. And I frequently want to append an item to the start of the list (with id 0) and want to delete the last entry (

5条回答
  •  野趣味
    野趣味 (楼主)
    2020-12-16 16:53

    Use collections.deque:

    >>> import collections
    >>> q = collections.deque(["herp", "derp", "blah", "what", "da.."])
    >>> q.appendleft('wuggah')
    >>> q.pop()
    'da..'
    >>> q
    deque(['wuggah', 'herp', 'derp', 'blah', 'what'])
    

提交回复
热议问题