Collections.deque()

Sort by

recency

|

645 Discussions

|

  • + 0 comments
    from collections import deque
    n = int(input())
    d = deque()
    for n in range(n):
        cmd = input().split()
        operation = cmd[0]
        if operation == "append":
            d.append(int(cmd[1]))
        elif operation == "appendleft":
            d.appendleft(int(cmd[1]))
        elif operation == "pop":
            d.pop()
        elif operation == "popleft":
            d.popleft()
            
    print(*d)
            
    
  • + 0 comments

    Here is my code

    from collections import deque
    
    q = deque()
    for _ in range( int(input()) ):
        operation, *value = input().split()
        # print(operation, value)
        method = getattr(q, operation)
        method(*value)
    
    print(*q)
    
  • + 0 comments

    from collections import deque n = int(input()) d = deque() for i in range(n): command,*args = input().split() if args: getattr(d,command)(int(args[0])) else: getattr(d,command)() print(' '.join(map(str,d)))

  • + 0 comments

    from collections import deque

    1. n = int(input())
    2. d = deque()
    3. for i in range(n):
    4. i = list(input().split())
    5. if (i[0]=="pop"):
    6. d.pop()
    7. elif(i[0]=="popleft"):
    8. d.popleft()
    9. else:
    10. x = i.pop(1)
    11. fun = getattr(d,i[0])
    12. fun(x)
    13. print(" ".join(d))
  • + 0 comments

    The fact that they’re memory efficient while maintaining great performance is another reason why they're a go-to in many scenarios. Definitely worth exploring the deque methods and recipes for anyone working with Python, 11winner register