How to use deque in Python

7 Answers

0 votes
import collections
 
d = collections.deque('python')     
 
print(d)      

print(d[0])      
print(d[-1])      



'''
run:

deque(['p', 'y', 't', 'h', 'o', 'n'])
p
n

'''

 



answered May 5, 2019 by avibootz
0 votes
from collections import deque

q = deque()
q.append('1')
q.append('18')
q.append(99)

print(q)
print(q[1])



   
'''
run:
    
deque(['1', '18', 99])
18
 
'''

 



answered Jul 21, 2020 by avibootz
0 votes
import collections 
  
q = collections.deque([1, 18, '27', 2, "python"]) 
  
q.append(8) 

print(q)
print(q[1])
print(q[4])



   
'''
run:
    
deque([1, 18, '27', 2, 'python', 8])
18
python
 
'''

 



answered Jul 21, 2020 by avibootz
0 votes
import collections 
  
q = collections.deque([1, 18, '27', 2, "python"]) 
  
q.appendleft(8) 

print(q)
print(q[1])
print(q[4])



   
'''
run:
    
deque([8, 1, 18, '27', 2, 'python'])
1
2
 
'''

 



answered Jul 21, 2020 by avibootz
0 votes
 import collections 
  
q = collections.deque([1, 18, '27', 2, 'python', 'java']) 

print(q)

q.pop() 

print(q)


   
'''
run:
    
deque([1, 18, '27', 2, 'python', 'java'])
deque([1, 18, '27', 2, 'python'])
 
'''

 



answered Jul 21, 2020 by avibootz
0 votes
import collections 
  
q = collections.deque([1, 18, '27', 2, 'python', 'java']) 

print(q)

q.popleft() 

print(q)


   
'''
run:
    
deque([1, 18, '27', 2, 'python', 'java'])
deque([18, '27', 2, 'python', 'java'])
 
'''

 



answered Jul 21, 2020 by avibootz
0 votes
import collections
 
lst = ['python', 'c', 'c++', 'javascript', 'java', "c#", "swift"]
 
dequeue = collections.deque(lst)
 
dequeue.appendleft("php")
 
print(dequeue)
print(dequeue[0])
 
 
 
'''
run:
 
deque(['php', 'python', 'c', 'c++', 'javascript', 'java', 'c#', 'swift'])
php
 
'''

 



answered May 4, 2024 by avibootz

Related questions

1 answer 113 views
113 views asked Nov 16, 2022 by avibootz
2 answers 175 views
1 answer 264 views
264 views asked Jul 21, 2020 by avibootz
1 answer 177 views
177 views asked Jul 21, 2020 by avibootz
1 answer 156 views
156 views asked Jul 21, 2020 by avibootz
...