Collections.deque()

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