HackerRankPythonPython InterviewRandom

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


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.

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.



>>> from collections import deque
>>> d = deque()
>>> d.append(1)
>>> print d
>>> d.appendleft(2)
>>> print d
deque([2, 1])
>>> d.clear()
>>> print d
>>> d.extend('1')
>>> print d
>>> d.extendleft('234')
>>> print d
deque(['4', '3', '2', '1'])
>>> d.count('1')
>>> d.pop()
>>> print d
deque(['4', '3', '2'])
>>> d.popleft()
>>> 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'])


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.


Output Format

Print the space separated elements of deque .

Sample Input

append 1
append 2
append 3
appendleft 4

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 STDOUT
from collections import deque
for i in range(N):
if A[0]=='append':
elif A[0]=='appendleft':
elif A[0]=='pop':
elif A[0]=='popleft':
for i in d:
print i,

Problem solution in Python 3 programming.

# Enter your code here. Read input from STDIN. Print output to STDOUT
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])

Problem solution in pypy programming.

# Enter your code here. Read input from STDIN. Print output to STDOUT
from collections import deque
d = deque()
for i in range(input()):
print ' '.join(map(str,d))

Problem solution in pypy3 programming.

# https://www.hackerrank.com/challenges/py-collections-deque?h_r=next-challenge&h_v=zen

from collections import deque

d = deque()
for i in range(int(input())):

operation, *number = input().split()
if number:
num1 = int(number[0])
if operation == 'append':
elif operation == 'appendleft':
elif operation == 'pop':
elif operation == 'popleft':

for i in range(len(d)):
print(d.popleft(), end = ' ')

Leave a Reply

10 Best Artificial Intelligence Software|artificial intelligence tools 5 nft games to earn money | Best NFT games for earn crypto Earn Money From Minting NFTs| How to mint NFT for free Top 10 Things You Need To Know About Python List | python lists functions 10 Popular PHP frameworks for web developers| best php frameworks 12 Tips On How To Become a Python Developer | python For beginner 12 Best Nodejs Frameworks for App Development in 2022 how to create google web stories, Steps to create web stories Top 10 Features in Angular 13 Every Developer Should Know | Angular 13 Features 10 Best Angular UI Libraries | angular ui components | angular Project 10 Best Web Development Frameworks in 2022 Frontend & Backend 18 Best Open-Source and Free Database Software | best database software for beginners Top 10+ Best Java IDEs & Online Java Compilers | best java ide for beginners top 10 besic to andvance java books |java books for beginer Top 5 Themes For Blogger, professional blogger theme download BEST Python Courses Online,Top 10 Courses to Learn Python in 2022 Top 13 python libraries for data science