How to print queue in Python

4 Answers

0 votes
from queue import Queue  
  
def printQueue(queue):
    i = 0
    while (i != queue.qsize()): 
        print(queue.queue[i]) 
        i = i + 1 
  
queue = Queue() 
    
queue.put(1)  
queue.put(2)  
queue.put(3)  
queue.put(4)  
queue.put(5)  

printQueue(queue) 
 
      
     
'''
run:

1
2
3
4
5
     
'''

 



answered Apr 4, 2020 by avibootz
0 votes
from queue import Queue  
  
q = Queue() 
    
q.put(1)  
q.put(2)  
q.put(3)  
q.put(4)  
q.put(5)  

print(list(q.queue))
 
      
     
'''
run:

[1, 2, 3, 4, 5]
     
'''

 



answered Apr 4, 2020 by avibootz
0 votes
from queue import Queue  
   
q = Queue() 
     
q.put(1)  
q.put(2)  
q.put(3)  
q.put(4)  
q.put(5)  
 
for item in q.queue:
    print(item)
  
       
      
'''
run:
 
1
2
3
4
5
      
'''

 



answered Apr 4, 2020 by avibootz
0 votes
from queue import Queue 
  
def PrintQueue(q): 
    while (not q.empty()):  
        print(q.queue[0], end=" ")  
        q.get() 


q = Queue()  

q.put(3)  
q.put(5)  
q.put(2)  
q.put(7)  
q.put(9)  
q.put(1)  
q.put(8)  

PrintQueue(q)

         
      
'''
run:
 
3 5 2 7 9 1 8
      
'''

 



answered Apr 16, 2020 by avibootz

Related questions

1 answer 180 views
180 views asked Jun 18, 2022 by avibootz
1 answer 199 views
1 answer 162 views
1 answer 280 views
1 answer 224 views
224 views asked Apr 4, 2020 by avibootz
...