collections.deque()

A deque is a double-ended queue. It can be used to add or remove elements from both ends.  

Deques support thread safe, memory efficient appends and pops from either side of the deque with approximately the same $O(1)$ performance in either direction.

Click on the link to learn more about deque() methods. 

Click on the link to learn more about various approaches to working with deques: Deque Recipes.

Example

Code

>>> from collections import deque
>>> d = deque()
>>> d.append(1)
>>> print d
deque([1])
>>> d.appendleft(2)
>>> print d
deque([2, 1])
>>> d.clear()
>>> print d
deque([])
>>> d.extend('1')
>>> print d
deque(['1'])
>>> d.extendleft('234')
>>> print d
deque(['4', '3', '2', '1'])
>>> d.count('1')
1
>>> d.pop()
'1'
>>> print d
deque(['4', '3', '2'])
>>> d.popleft()
'4'
>>> print d
deque(['3', '2'])
>>> d.extend('7896')
>>> print d
deque(['3', '2', '7', '8', '9', '6'])
>>> d.remove('2')
>>> print d
deque(['3', '7', '8', '9', '6'])
>>> d.reverse()
>>> print d
deque(['6', '9', '8', '7', '3'])
>>> d.rotate(3)
>>> print d
deque(['8', '7', '3', '6', '9'])

Task  

Perform append, pop, popleft and appendleft methods on an empty deque $\boldsymbol{d}$.

Input Format

The first line contains an integer $N$, the number of operations. 

The next $N$ lines contains the space separated names of methods and their values.  

Constraints

$0<N\leq100$

Output Format

Print the space separated elements of deque $\boldsymbol{d}$.

Sample Input
6
append 1
append 2
append 3
appendleft 4
pop
popleft

Sample Output
1 2

Difficulty: EASY

Solutions:
from collections import deque
n = int(input())
d = deque()
for i in range(n):
	command = input().strip().split()
	if len(command) == 2:
		getattr(d, command[0])(command[1])
	else:
		getattr(d, command[0])()
print(' '.join(d))


from collections import deque
d = deque()
n = int(input())
for i in range(n):
	inp = input().split(' ')
	com = inp[0]
	if com == 'append':
		d.append(int(inp[1]))
	elif com == 'popleft':
		d.popleft()
	elif com == 'appendleft':
		d.appendleft(int(inp[1]))
	else:
		d.pop()
for e in d:
	print(e, end=' ')


from collections import deque
n = int(input())
d = deque()
for i in range(n):
	x = input().split()
	if x[0] == 'append':
		d.append(x[1])
	elif x[0] == 'appendleft':
		d.appendleft(x[1])
	elif x[0] == 'pop':
		d.pop()
	elif x[0] == 'popleft':
		d.popleft()
for i in d:
	print(i, end=' ')


import collections
n = int(input())
d = collections.deque()
for i in range(n):
	cmd = list(input().strip().split())
	opt = cmd[0]
	if opt == 'pop':
		d.pop()
	elif opt == 'popleft':
		d.popleft()
	elif opt == 'append':
		d.append(int(cmd[1]))
	elif opt == 'appendleft':
		d.appendleft(int(cmd[1]))
for i in d:
	print(i, end=' ')


from collections import deque
n = int(input())
d = deque()
for _ in range(n):
	spl_str = input().strip().split()
	operation = spl_str[0]
	if len(spl_str) == 2:
		value = spl_str[1]
	else:
		value = None
	getattr(d, operation)(value) if value else getattr(d, operation)()
print(' '.join(d))


from collections import deque
n = int(input())
d = deque()
for i in range(n):
	items = input().split()
	if items[0] == 'append':
		d.append(int(items[1]))
	elif items[0] == 'appendleft':
		d.appendleft(int(items[1]))
	elif items[0] == 'pop':
		d.pop()
	elif items[0] == 'popleft':
		d.popleft()
print(*list(d))


from collections import deque
n = int(input())
d = deque()
for i in range(n):
	s = input()
	sl = list(map(lambda x: x.strip(), s.split(' ')))
	op = sl[0]
	if op in ['append', 'appendleft']:
		v = sl[1]
		if op == 'append':
			d.append(v)
		if op == 'appendleft':
			d.appendleft(v)
	else:
		if op == 'pop':
			d.pop()
		if op == 'popleft':
			d.popleft()
print(' '.join(list(d)))


from collections import deque
funcdict = {'append': deque.append, 'appendleft': deque.appendleft, 'pop': deque.pop, 'popleft': deque.popleft}
n = int(input())
d = deque()
for i in range(0, n):
	data = input().split()
	command = data[0]
	if len(data) == 2:
		funcdict[command](d, int(data[1]))
	else:
		funcdict[command](d)
for i in d:
	print(i, end=' ')


from collections import deque
n = int(input())
d = deque()
for _ in range(n):
	(cmd, *rest) = input().strip().split()
	s = ''.join(rest)
	eval('d.{}({})'.format(cmd, s))
print(' '.join((str(x) for x in d)))


from collections import deque
N = int(input())
d = deque()
for i in range(N):
	command = input().split()
	if len(command) == 2:
		eval('d.' + command[0] + '(' + command[1] + ')')
	else:
		eval('d.' + command[0] + '()')
print(' '.join(map(str, [elem for elem in d])))


from collections import deque
d = deque()
for _ in range(int(input())):
	cmd = input().split()
	if len(cmd) > 1:
		getattr(d, cmd[0])(int(cmd[1]))
	else:
		getattr(d, cmd[0])()
print(*d)


from collections import deque
d = deque()
N = int(input())
for _ in range(N):
	line = input().split()
	getattr(d, line[0])(*list(map(int, line[1:])))
print(' '.join([str(x) for x in d]))


from collections import deque
n = int(input())
d = deque()
for _ in range(n):
	ins = input().split()
	if ins[0] == 'append':
		d.append(ins[-1])
	elif ins[0] == 'appendleft':
		d.appendleft(ins[-1])
	elif ins[0] == 'pop':
		d.pop()
	else:
		d.popleft()
print(' '.join([x for x in d]))


from collections import deque
d = deque()
for k in range(0, int(input())):
	args = input().split()
	if len(args) == 1:
		getattr(d, args[0])()
	else:
		getattr(d, args[0])(int(args[1]))
print(' '.join([str(x) for x in d]))


from collections import deque
d = deque()
for _ in range(int(input())):
	s = input().split()
	exec('{}.{}({})'.format('d', s[0], s[1] if len(s) > 1 else ''))
print(' '.join([str(x) for x in d]))


from collections import deque
x = deque()
for i in range(int(input())):
	inp = input().strip().split()
	eval('x.' + str(inp[0]) + '(' + ','.join(inp[1:]) + ')')
print(' '.join(map(str, x)))


from collections import deque
d = deque()
x = int(input())
for i in range(x):
	(cmd, *args) = input().split()
	getattr(d, cmd)(*args)
[print(x, end=' ') for x in d]


from collections import deque
a = int(input())
d = deque()
for i in range(0, a):
	tmp = input().split()
	getattr(d, tmp[0])(*([tmp[1]] if len(tmp) > 1 else []))
print(*[item for item in d])


from collections import deque
N = int(input())
d = deque()
for _ in range(N):
	command = input().split()
	if command[0] == 'pop':
		d.pop()
	elif command[0] == 'popleft':
		d.popleft()
	elif command[0] == 'append':
		d.append(command[1])
	elif command[0] == 'appendleft':
		d.appendleft(command[1])
print(*d)


from collections import deque
ls = deque()
nb = int(input())
for _ in range(nb):
	a = input().strip()
	if 'pop' == a:
		ls.pop()
	elif 'popleft' == a:
		ls.popleft()
	else:
		(a, b) = a.split()
		if 'append' == a:
			ls.append(int(b))
		else:
			ls.appendleft(int(b))
print(*list(ls))


from collections import deque
nops = int(input())
q = deque()
for _ in range(nops):
	action = input().split()
	if len(action) == 2:
		(operation, operand) = action
		getattr(q, operation)(int(operand))
	else:
		getattr(q, action[0])()
print(' '.join((str(num) for num in q)))


from collections import deque
d = deque()
for i in range(int(input())):
	temp = input().strip().split(' ')
	if temp[0] == 'append':
		d.append(int(temp[1]))
	elif temp[0] == 'appendleft':
		d.appendleft(int(temp[1]))
	elif temp[0] == 'pop':
		d.pop()
	elif temp[0] == 'popleft':
		d.popleft()
print(' '.join(map(str, d)))


from collections import deque
d = deque()
n = int(input())
for i in range(n):
	cmd = input().split()
	if cmd[0] == 'append':
		d.append(int(cmd[1]))
	elif cmd[0] == 'appendleft':
		d.appendleft(int(cmd[1]))
	elif cmd[0] == 'pop':
		d.pop()
	elif cmd[0] == 'popleft':
		d.popleft()
for i in d:
	print(i, end=' ')


from collections import deque
d = deque()
n = int(input())
for i in range(n):
	s = input().split()
	if s[0] == 'append':
		d.append(int(s[1]))
	elif s[0] == 'appendleft':
		d.appendleft(int(s[1]))
	elif s[0] == 'pop':
		d.pop()
	elif s[0] == 'popleft':
		d.popleft()
for i in d:
	print(i, end=' ')


from collections import deque
N = int(input())
d = deque()
for each in range(N):
	o = input().strip()
	if ' ' in o:
		(o, q) = o.split()
	if o == 'append':
		d.append(q)
	elif o == 'appendleft':
		d.appendleft(q)
	elif o == 'clear':
		d.clear()
	elif o == 'count':
		d.count(q)
	elif o == 'extend':
		d.extend(q)
	elif o == 'extendleft':
		d.extendleft(q)
	elif o == 'pop':
		d.pop()
	elif o == 'popleft':
		d.popleft()
	elif o == 'remove':
		d.remove(q)
	elif o == 'reverse':
		d.reverse()
	elif o == 'rotate':
		d.rotate(q)
	else:
		d.pop()
for each in range(len(d)):
	print(d[each], end=' ')


from collections import deque
d = deque()
for _ in range(int(input())):
	(c, *arg) = input().split()
	getattr(d, c)(*arg)
print(*d)


from collections import deque
d = deque()
for _ in range(int(input())):
	(cmd, *args) = input().split()
	getattr(d, cmd)(*args)
[print(x, end=' ') for x in d]


from collections import deque
q = deque()
for _ in range(int(input())):
	a = input().split()
	getattr(q, a[0])(*map(int, a[1:]))
print(' '.join(map(str, q)))


from collections import deque
d = deque()
for _ in range(int(input())):
	a = input().split()
	if a[0] == 'append':
		d.append(a[1])
	if a[0] == 'appendleft':
		d.appendleft(a[1])
	if a[0] == 'pop':
		d.pop()
	if a[0] == 'popleft':
		d.popleft()
for i in d:
	print(i, end=' ')


from collections import deque
N = int(input())
d = deque()
for i in range(N):
	x = input().split()
	if x[0] == 'pop':
		d.pop()
	elif x[0] == 'popleft':
		d.popleft()
	elif x[0] == 'append':
		d.append(int(x[1]))
	else:
		d.appendleft(int(x[1]))
for i in d:
	print(i, end=' ')


from collections import deque
d = deque()
for i in range(int(input())):
	s = input().split()
	if s[0] in {'pop', 'popleft'}:
		eval('d.' + s[0] + '()')
	else:
		eval('d.' + s[0] + '(' + s[1] + ')')
print(' '.join(map(str, d)))


from collections import deque
N = int(input())
que = deque()
for _ in range(N):
	line = input().split()
	if 'append' in line:
		que.append(int(line[1]))
	elif 'appendleft' in line:
		que.appendleft(int(line[1]))
	elif 'pop' in line:
		que.pop()
	elif 'popleft' in line:
		que.popleft()
for item in que:
	print(item, end=' ')


from collections import deque
N = int(input())
d = deque()
for _ in range(N):
	args = input().split()
	if args[0] == 'append':
		d.append(int(args[1]))
	elif args[0] == 'pop':
		d.pop()
	elif args[0] == 'popleft':
		d.popleft()
	else:
		d.appendleft(int(args[1]))
print(*d, sep=' ')


from collections import deque
q = deque()
n = int(input())
for i in range(n):
	IV = input().strip().split(' ')
	if len(IV) == 1:
		instr = IV[0]
	else:
		(instr, val) = IV
	if instr == 'append':
		q.append(val)
	elif instr == 'pop':
		q.pop()
	elif instr == 'popleft':
		q.popleft()
	elif instr == 'appendleft':
		q.appendleft(val)
for i in q:
	print(i, end=' ')


from collections import deque
q = deque()
for _ in range(int(input())):
	l = input().split()
	if l[0] == 'append':
		q.append(int(l[1]))
	elif l[0] == 'appendleft':
		q.appendleft(int(l[1]))
	elif l[0] == 'pop':
		q.pop()
	elif l[0] == 'popleft':
		q.popleft()
print(*q)


from collections import deque

def __starting_point():
	num = int(input().strip())
	deq = deque()
	for _ in range(num):
		args = input().strip().split()
		if args[0] == 'append':
			deq.append(args[1])
		elif args[0] == 'appendleft':
			deq.appendleft(args[1])
		elif args[0] == 'pop':
			deq.pop()
		elif args[0] == 'popleft':
			deq.popleft()
	out = []
	for el in deq:
		out.append(el)
	print(' '.join(map(str, out)))
__starting_point()


from collections import deque
n = int(input())
d = deque()
for _ in range(n):
	instruction = input().strip()
	if instruction.startswith('appendleft'):
		(_, value) = instruction.split()
		d.appendleft(value)
	elif instruction == 'popleft':
		d.popleft()
	elif instruction.startswith('append'):
		(_, value) = instruction.split()
		d.append(value)
	elif instruction == 'pop':
		d.pop()
print(' '.join(d))


from collections import deque
n_ops = int(input())
data = deque()
for _ in range(n_ops):
	s = input().split()
	if s[0] == 'append':
		data.append(int(s[1]))
	elif s[0] == 'appendleft':
		data.appendleft(int(s[1]))
	elif s[0] == 'pop':
		data.pop()
	elif s[0] == 'popleft':
		data.popleft()
print(*data)


from collections import deque
n = int(input())
d = deque()
for _ in range(n):
	args = input().split()
	if 'pop' in args[0]:
		getattr(d, args[0])()
	else:
		getattr(d, args[0])(int(args[1]))
for i in d:
	print(i, end=' ')


from collections import deque
N = int(input())
q = deque()
for i in range(N):
	inp = input().split()
	if inp[0] == 'append':
		q.append(int(inp[1]))
	elif inp[0] == 'appendleft':
		q.appendleft(int(inp[1]))
	elif inp[0] == 'pop':
		q.pop()
	elif inp[0] == 'popleft':
		q.popleft()
for i in q:
	print(i, end=' ')


from collections import deque
d = deque()
nips = int(input())
for i in range(0, nips):
	ip = list(map(str, input().split()))
	if len(ip) == 2:
		ch = ip[1]
	if ip[0] == 'append':
		d.append(int(ch))
	if ip[0] == 'appendleft':
		d.appendleft(int(ch))
	if ip[0] == 'clear':
		d.clear()
	if ip[0] == 'extend':
		for val in ch:
			d.extend(int(val))
	if ip[0] == 'extendleft':
		for val in ch:
			d.extendleft(int(val))
	if ip[0] == 'pop':
		d.pop()
	if ip[0] == 'popleft':
		d.popleft()
	if ip[0] == 'remove':
		d.remove(int(ch))
	if ip[0] == 'rotate':
		d.rotate(int(ch))
	if ip[0] == 'reverse':
		d.reverse()
for val in d:
	print(val, end=' ')


from collections import deque
d = deque()
for _ in range(int(input())):
	l = input().split()
	getattr(d, l[0])(*([l[1]] if len(l) > 1 else []))
print(*list(d))


from collections import deque
d = deque()
for _ in range(int(input())):
	inp = input().split()
	getattr(d, inp[0])(*([inp[1]] if len(inp) > 1 else []))
print(*[item for item in d])


from collections import deque
action_count = int(input())
my_deque = deque()
for _ in range(0, action_count):
	this_line = input().split()
	action_name = this_line[0]
	action_arg = 0
	if len(this_line) > 1:
		action_arg = int(this_line[1])
	if action_name == 'append':
		my_deque.append(action_arg)
	elif action_name == 'appendleft':
		my_deque.appendleft(action_arg)
	elif action_name == 'pop':
		my_deque.pop()
	elif action_name == 'popleft':
		my_deque.popleft()
print(*my_deque)


from collections import deque
d = deque()
for i in range(int(input())):
	temp = input().split()
	command = temp[0]
	if len(temp) > 1:
		num = int(temp[1])
	if command == 'append':
		d.append(num)
	elif command == 'appendleft':
		d.appendleft(num)
	elif command == 'pop':
		d.pop()
	elif command == 'popleft':
		d.popleft()
	else:
		pass
print(*d)


import collections
import sys
N = int(sys.stdin.readline())
d = collections.deque()
for _ in range(N):
	cmd = sys.stdin.readline().strip().split()
	if cmd[0] == 'append':
		d.append(int(cmd[1]))
	elif cmd[0] == 'appendleft':
		d.appendleft(int(cmd[1]))
	elif cmd[0] == 'pop':
		d.pop()
	elif cmd[0] == 'popleft':
		d.popleft()
print(*d)


from collections import deque
N = int(input())
dq = deque()
for _ in range(N):
	(command, *args) = input().split()
	args = map(int, args)
	if command == 'append':
		dq.append(*args)
	elif command == 'appendleft':
		dq.appendleft(*args)
	elif command == 'pop':
		dq.pop(*args)
	elif command == 'popleft':
		dq.popleft(*args)
print(*dq)


from collections import deque
d = deque()
for _ in range(int(input())):
	command = input().split()
	if command[0] == 'append':
		d.append(command[1])
	elif command[0] == 'appendleft':
		d.appendleft(command[1])
	elif command[0] == 'pop':
		d.pop()
	elif command[0] == 'popleft':
		d.popleft()
print(' '.join(d))


from collections import deque
D = deque()
for i in range(int(input())):
	a = input().rstrip().split()
	if a[0] == 'append':
		D.append(a[-1])
	if a[0] == 'pop':
		D.pop()
	if a[0] == 'popleft':
		D.popleft()
	if a[0] == 'appendleft':
		D.appendleft(a[-1])
print(*D)


from collections import deque
d = deque()
n = int(input())
for _ in range(n):
	s = input().split()
	getattr(d, s[0])(*s[1:])
print(' '.join(d))


from collections import deque
a = deque()
for i in range(int(input())):
	b = input().split()
	if len(b) == 2:
		getattr(a, b[0])(b[1])
	else:
		getattr(a, b[0])()
print(' '.join(a))


from collections import deque
d = deque()
for i in range(0, int(input())):
	c = input().split()
	if len(c) == 2:
		exec('d.' + c[0] + '(' + c[1] + ')')
	elif len(c) == 1:
		exec('d.' + c[0] + '(' + ')')
for i in d:
	print(i, end=' ')


from collections import deque
q = deque()
for _ in range(int(input())):
	a = input().split()
	if a[0] == 'append':
		q.append(a[1])
	elif a[0] == 'appendleft':
		q.appendleft(a[1])
	elif a[0] == 'pop':
		q.pop()
	elif a[0] == 'popleft':
		q.popleft()
	else:
		print('Unknown commad %s' % a[0])
print(' '.join(q))


from collections import deque
d = deque()
for i in range(int(input())):
	inp = input().strip()
	if inp != 'pop' and inp != 'popleft':
		(comm, val) = inp.split(' ')
		val = int(val)
	else:
		comm = inp
	if comm.__eq__('append'):
		d.append(val)
	elif comm.__eq__('pop'):
		d.pop()
	elif comm.__eq__('popleft'):
		d.popleft()
	elif comm.__eq__('appendleft'):
		d.appendleft(val)
for i in d:
	print(i, end=' ')


from collections import deque
d = deque()
for i in range(int(input())):
	s = input().split()
	if s[0] == 'pop':
		d.pop()
	elif s[0] == 'popleft':
		d.popleft()
	elif s[0] == 'append':
		d.append(int(s[1]))
	elif s[0] == 'appendleft':
		d.appendleft(int(s[1]))
print(*d)


from collections import deque
d = deque()
for i in range(int(input())):
	inputs = input().strip().split(' ')
	command = inputs[0]
	if command == 'append':
		d.append(inputs[1])
	if command == 'appendleft':
		d.appendleft(inputs[1])
	if command == 'pop':
		d.pop()
	if command == 'popleft':
		d.popleft()
print(' '.join(d))


from collections import deque
queue = deque()
N = int(input())
for _ in range(N):
	s = input()
	if 'appendleft' in s:
		queue.appendleft(int(s.split()[1]))
	elif 'append' in s:
		queue.append(int(s.split()[1]))
	elif 'popleft' in s:
		queue.popleft()
	elif 'pop' in s:
		queue.pop()
print(*queue)


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


from collections import deque
n = int(input())
d = deque()
for i in range(n):
	comm = input().strip().split()
	if len(comm) > 1:
		getattr(d, comm[0])(int(comm[1]))
	else:
		getattr(d, comm[0])()
for i in d:
	print(i, end=' ')


from collections import deque

def main():
	n = int(input())
	q = deque()
	for _ in range(n):
		s = input().split()
		if s[0] == 'append':
			q.append(s[1])
		elif s[0] == 'appendleft':
			q.appendleft(s[1])
		elif s[0] == 'pop':
			q.pop()
		elif s[0] == 'popleft':
			q.popleft()
	print(' '.join((q.popleft() for i in range(len(q)))))
main()


from collections import deque
d = deque()
N = int(input())
for _ in range(N):
	s = [x for x in input().split()]
	if s[0] == 'append':
		d.append(s[1])
	elif s[0] == 'appendleft':
		d.appendleft(s[1])
	elif s[0] == 'pop':
		d.pop()
	elif s[0] == 'popleft':
		d.popleft()
print(' '.join(list(d)))


from collections import deque
x = int(input())
d = deque()
for i in range(x):
	method_parametr = input().split()
	method = getattr(d, method_parametr[0])
	if len(method_parametr) > 1:
		method(int(method_parametr[1]))
	else:
		method()
print(' '.join(list(map(str, d))))


from collections import deque
d = deque()
for _ in range(int(input())):
	i = input().split()
	if i[0] == 'append':
		d.append(int(i[1]))
	elif i[0] == 'appendleft':
		d.appendleft(int(i[1]))
	elif i[0] == 'pop':
		d.pop()
	elif i[0] == 'popleft':
		d.popleft()
print(' '.join([str(s) for s in d]))


from collections import deque
d = deque()
N = int(input())
for n in range(N):
	cmd = input().strip().split(' ')
	if cmd[0] == 'append':
		d.append(cmd[1])
	elif cmd[0] == 'pop':
		d.pop()
	elif cmd[0] == 'popleft':
		d.popleft()
	elif cmd[0] == 'appendleft':
		d.appendleft(cmd[1])
print(' '.join(d))


import collections as col
d = col.deque()
N = int(input())
for i in range(N):
	op = input().split()
	if op[0] == 'append':
		d.append(op[1])
	if op[0] == 'appendleft':
		d.appendleft(op[1])
	if op[0] == 'pop':
		d.pop()
	if op[0] == 'popleft':
		d.popleft()
print(' '.join(d))


from collections import deque
operations = int(input())
theDeque = deque()
for _ in range(0, operations):
	theCommand = input().split()
	if theCommand[0] == 'append':
		theDeque.append(int(theCommand[1]))
	elif theCommand[0] == 'appendleft':
		theDeque.appendleft(int(theCommand[1]))
	elif theCommand[0] == 'pop':
		theDeque.pop()
	elif theCommand[0] == 'popleft':
		theDeque.popleft()
for i in theDeque:
	print(i, end=' ')


from collections import deque
n = int(input())
d = deque()
for _ in range(n):
	lst = list(input().split())
	if lst[0] == 'append':
		d.append(int(lst[1]))
	elif lst[0] == 'appendleft':
		d.appendleft(int(lst[1]))
	elif lst[0] == 'popleft':
		d.popleft()
	elif lst[0] == 'pop':
		d.pop()
for i in d:
	print(i, end=' ')


from collections import deque
d = deque()
N = int(input())
for _ in range(N):
	i = input().split()
	(method, arg) = i if len(i) == 2 else (i[0], None)
	if arg:
		getattr(d, method)(int(arg))
	else:
		getattr(d, method)()
print(*d)


from collections import deque
(n, d) = (int(input()), deque())
for _ in range(n):
	line = input().strip().split()
	if len(line) == 2:
		getattr(d, line[0])(line[1])
	else:
		getattr(d, line[0])()
print(' '.join(d))


import collections
deq = collections.deque()
for line in range(int(input())):
	(command, *args) = input().split()
	getattr(deq, command)(*args)
print(*deq)


from collections import deque
d = deque()
for _ in range(int(input())):
	operation = input().strip()
	if ' ' in operation:
		command = 'd.' + operation.replace(' ', '(') + ')'
	else:
		command = 'd.' + operation + '()'
	eval(command)
for i in d:
	print(i, end=' ')


import collections
d = collections.deque()
ncmd = int(input())
for _ in range(ncmd):
	(command, _, args) = input().strip().partition(' ')
	args = args.split()
	getattr(d, command)(*map(int, args))
print(' '.join(map(str, d)))


from collections import deque
n = int(input())
d = deque()
for i in range(0, n):
	inp = input().split()
	op = inp[0]
	if op == 'append':
		value = inp[1]
		d.append(value)
	elif op == 'pop':
		d.pop()
	elif op == 'popleft':
		d.popleft()
	elif op == 'appendleft':
		value = inp[1]
		d.appendleft(value)
for value in d:
	print(value, end=' ')


from collections import deque
d = deque()
for i in range(int(input())):
	line = input().split()
	if line[0] == 'append':
		d.append(int(line[1]))
	elif line[0] == 'appendleft':
		d.appendleft(int(line[1]))
	elif line[0] == 'popleft':
		d.popleft()
	else:
		d.pop()
print(*d)


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


from collections import deque
N = int(input())
numbers = deque()
for _ in range(N):
	line = input().split()
	command = line[0]
	if command == 'pop':
		x = numbers.pop()
	elif command == 'popleft':
		x = numbers.popleft()
	else:
		value = line[1]
		if command == 'append':
			numbers.append(value)
		elif command == 'appendleft':
			numbers.appendleft(value)
[print(numbers[i], end=' ') for i in range(len(numbers))]


from collections import deque
N = int(input())
d = deque()
for _ in range(N):
	command = input().split()
	if command[0] == 'append':
		d.append(int(command[1]))
	elif command[0] == 'appendleft':
		d.appendleft(int(command[1]))
	elif command[0] == 'pop':
		d.pop()
	elif command[0] == 'popleft':
		d.popleft()
	else:
		print('Syntax Error:', operation, 'invalid operation.')
print(' '.join(list(map(str, d))))


import collections
N = int(input())
d = collections.deque()
for n in range(N):
	line = input().split()
	if line[0] == 'pop':
		d.pop()
	elif line[0] == 'popleft':
		d.popleft()
	elif line[0] == 'append':
		d.append(int(line[1]))
	elif line[0] == 'appendleft':
		d.appendleft(int(line[1]))
for v in d:
	print(v, end=' ')


from collections import deque
d = deque()
for i in range(int(input())):
	line = input().split()
	if line[0] == 'append':
		d.append(int(line[-1]))
	elif line[0] == 'pop':
		d.pop()
	elif line[0] == 'popleft':
		d.popleft()
	elif line[0] == 'appendleft':
		d.appendleft(line[-1])
print(' '.join(list(map(str, d))))


from collections import deque
d = deque()
N = int(input())
for i in range(N):
	text = input().split()
	op = text[0]
	if op == 'append':
		d.append(text[1])
	elif op == 'appendleft':
		d.appendleft(text[1])
	elif op == 'pop':
		d.pop()
	elif op == 'popleft':
		d.popleft()
for i in d:
	print(i, end=' ')


from collections import deque
d = deque()
for _ in range(int(input())):
	tokens = input().split()
	if len(tokens) == 2:
		getattr(d, tokens[0])(int(tokens[1]))
	else:
		getattr(d, tokens[0])()
print(' '.join(map(str, d)))


from collections import deque
d = deque()
for _ in range(int(input())):
	r = list(input().split())
	if r[0] == 'append':
		d.append(r[1])
	elif r[0] == 'appendleft':
		d.appendleft(r[1])
	elif r[0] == 'pop':
		d.pop()
	else:
		d.popleft()
print(' '.join(d))


from collections import deque
d = deque()
for _ in range(int(input())):
	command = input().split()
	if command[0] == 'append':
		d.append(int(command[1]))
	elif command[0] == 'appendleft':
		d.appendleft(int(command[1]))
	elif command[0] == 'pop':
		d.pop()
	elif command[0] == 'popleft':
		d.popleft()
for el in d:
	print(el, end=' ')


from collections import deque

def solve():
	t = int(input())
	a = deque()
	for i in range(0, t):
		s = input().strip()
		if s[0] == 'a':
			(op, v) = s.split()
			if op == 'append':
				a.append(int(v))
			else:
				a.appendleft(int(v))
		elif s == 'pop':
			a.pop()
		else:
			a.popleft()
	for i in a:
		print(i, end=' ')
	print()
solve()


from collections import deque
deq = deque()
n = int(input().strip())
for i in range(n):
	inst = input().strip()
	if inst == 'pop':
		deq.pop()
	elif inst == 'popleft':
		deq.popleft()
	else:
		(inst, x) = inst.split()
	if inst == 'append':
		deq.append(x)
	elif inst == 'appendleft':
		deq.appendleft(x)
print(' '.join(deq))


from collections import deque
d = deque()
for _ in range(int(input())):
	(meth, *t) = input().split()
	t = [int(x) for x in t]
	getattr(d, meth)(*t)
for x in d:
	print(x, end=' ')


from collections import deque
d = deque()
for _ in range(0, int(input().strip())):
	a = input().strip().split(' ')
	if a[0] == 'append':
		d.append(a[1])
	elif a[0] == 'pop':
		d.pop()
	elif a[0] == 'popleft':
		d.popleft()
	elif a[0] == 'appendleft':
		d.appendleft(a[1])
print(' '.join(d))


import collections

def main():
	N = int(input())
	d = collections.deque()
	for _ in range(N):
		(command, *args) = input().split()
		getattr(d, command)(*args)
	print(*d)
main()


import collections
n = int(input())
d = collections.deque()
for i in range(n):
	l = list(input().strip().split())
	if len(l) == 1:
		exec('d.' + l[0] + '()')
	else:
		exec('d.' + l[0] + '(' + l[1] + ')')
for i in d:
	print(i, end=' ')


from collections import deque
d = deque()
for _ in range(0, int(input())):
	ls = input().split()
	if len(ls) == 1:
		getattr(d, ls[0])()
	else:
		getattr(d, ls[0])(ls[1])
print(*d, sep=' ')


from collections import deque
n = int(input())
q = deque()
for _ in range(n):
	(command, *args) = input().split()
	getattr(q, command)(*args)
print(' '.join(q))


from collections import deque
d = deque()
t = int(input())
for a0 in range(t):
	a = input().strip().split(' ')
	if a[0] == 'append':
		d.append(a[1])
	if a[0] == 'appendleft':
		d.appendleft(a[1])
	if a[0] == 'pop':
		d.pop()
	if a[0] == 'popleft':
		d.popleft()
print(*d, sep=' ')


from collections import deque
N = int(input())
d = deque()
for _ in range(N):
	task = input().split()
	if task[0] == 'append':
		d.append(task[1])
	if task[0] == 'pop':
		d.pop()
	if task[0] == 'appendleft':
		d.appendleft(task[1])
	if task[0] == 'popleft':
		d.popleft()
print(' '.join(d))