# HackerRank Collections.deque() solution in python | python problem solution

### collections.deque()

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  performance in either direction.

Example

Code

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

Perform appendpoppopleft and appendleft methods on an empty deque .

Input Format

The first line contains an integer , the number of operations.
The next  lines contains the space separated names of methods and their values.

Constraints

Output Format

Print the space separated elements of deque .

Sample Input

``6append 1append 2append 3appendleft 4poppopleft``

Sample Output

``1 2``
``In this Collections.deque() problem we need to develop a python program that can read integer and space separated methods on the next line. and we need to print the space-separated elements on the output screen.``
``````Problem solution in Python 2 programming.# Enter your code here. Read input from STDIN. Print output to STDOUTfrom collections import dequed=deque()N=int(raw_input())for i in range(N):    A=list(raw_input().split())    if A=='append':        d.append(int(A))    elif A=='appendleft':        d.appendleft(int(A))    elif A=='pop':        d.pop()    elif A=='popleft':        d.popleft()for i in d:    print i,
```
```Problem solution in Python 3 programming.# Enter your code here. Read input from STDIN. Print output to STDOUTfrom collections import dequed = deque()for _ in range(int(input())):    inp = input().split()    getattr(d, inp)(*[inp] if len(inp) > 1 else [])print(*[item for item in d])

Problem solution in pypy programming.
# Enter your code here. Read input from STDIN. Print output to STDOUTfrom collections import dequed = deque()for i in range(input()):    eval('d.{0}({1})'.format(*raw_input().split()+['']))print ' '.join(map(str,d))

Problem solution in pypy3 programming.
# https://www.hackerrank.com/challenges/py-collections-deque?h_r=next-challenge&h_v=zenfrom collections import dequed = deque()for i in range(int(input())):        operation, *number = input().split()    if number:        num1 = int(number)    if operation == 'append':        d.append(num1)    elif operation == 'appendleft':        d.appendleft(num1)    elif operation == 'pop':        d.pop()    elif operation == 'popleft':        d.popleft()    for i in range(len(d)):    print(d.popleft(), end = ' ')
```

```