Open In App

Dumping queue into list or array in Python

Last Updated : 31 Dec, 2020
Improve
Improve
Like Article
Like
Save
Share
Report

Prerequisite: Queue in Python

Here given a queue and our task is to dump the queue into list or array. We are to see two methods to achieve the objective of our solution.

Example 1: 

In this example, we will create a queue using the collection package and then cast it into the list

Python3




# Python program to
# demonstrate queue implementation
# using collections.dequeue
   
from collections import deque
   
# Initializing a queue
q = deque()
   
# Adding elements to a queue
q.append('a')
q.append('b')
q.append('c')
  
# display the queue
print("Initial queue")
print(q,"\n")
  
# display the type
print(type(q))


Output:

Initial queue
deque(['a', 'b', 'c']) 

<class 'collections.deque'>

Let’s create a list and cast into it:

Python3




# convert into list
li = list(q)
  
# display
print("Convert into the list")
print(li)
print(type(li))


Output:

Convert into the list
['a', 'b', 'c']
<class 'list'>

Example 2:

In this example, we will create a queue using the queue module and then cast it into the list.

Python3




from queue import Queue
  
# Initializing a queue
que = Queue()
  
# Adding elements to a queue
que.put(1)
que.put(2)
que.put(3)
que.put(4)
que.put(5)
  
# display the queue
print("Initial queue")
print(que.queue)
  
# casting into the list
li = list(que.queue)
print("\nConverted into the list")
print(li)


Output:

Initial queue
deque([1, 2, 3, 4, 5])

Converted into the list
[1, 2, 3, 4, 5]


Previous Article
Next Article

Similar Reads

PyQt5 QSpinBox - Dumping object information
In this article we will see how we can dump the object information of the spin box, dumping object information means to dump the information about signal connections, etc. for the spin box to debug the output. In order to do this we use dumpObjectInfo method Syntax : spin_box.dumpObjectInfo() Argument : It takes no argument Return : It returns None
2 min read
PyQt5 QSpinBox - Dumping object tree
In this article we will see how we can dump the object tree of the spin box, dumping object tree means to dump a tree of children to the debug output. Object tree is a tree data structure holding child objects of spin box at the nodes. In order to do this we use dumpObjectTree method Syntax : spin_box.dumpObjectTree() Argument : It takes no argumen
2 min read
Should we declare as Queue or Priority Queue while using Priority Queue in Java?
Queue: Queue is an Interface that extends the collection Interface in Java and this interface belongs to java.util package. A queue is a type of data structure that follows the FIFO (first-in-first-out ) order. The queue contains ordered elements where insertion and deletion of elements are done at different ends. Priority Queue and Linked List are
3 min read
Check if a queue can be sorted into another queue using a stack
Given a Queue consisting of first n natural numbers (in random order). The task is to check whether the given Queue elements can be arranged in increasing order in another Queue using a stack. The operation allowed are: Push and pop elements from the stack Pop (Or Dequeue) from the given Queue. Push (Or Enqueue) in the another Queue. Examples : Inp
9 min read
Turn a Queue into a Priority Queue
What is Queue?Queue is an abstract data type that is open at both ends. One end is always used to insert data (enqueue) which is basically the rear/back/tail end and the other which is the front end is used to remove data (dequeue). Queue follows First-In-First-Out (FIFO) methodology, i.e., "the data item stored first will be accessed first". Decla
9 min read
Stack and Queue in Python using queue Module
A simple python List can act as queue and stack as well. Queue mechanism is used widely and for many purposes in daily life. A queue follows FIFO rule(First In First Out) and is used in programming for sorting and for many more things. Python provides Class queue as a module which has to be generally created in languages such as C/C++ and Java. 1.
3 min read
Priority Queue using Queue and Heapdict module in Python
Priority Queue is an extension of the queue with the following properties. An element with high priority is dequeued before an element with low priority. If two elements have the same priority, they are served according to their order in the queue. queue.PriorityQueue(maxsize) It is a constructor for a priority queue. maxsize is the number of eleme
3 min read
Difference between queue.queue vs collections.deque in Python
Both queue.queue and collections.deque commands give an idea about queues in general to the reader but, both have a very different application hence shouldn't be confused as one. Although they are different and used for very different purposes they are in a way linked to each other in terms of complete functionality. Before we jump into what they a
3 min read
Python multiprocessing.Queue vs multiprocessing.manager().Queue()
In Python, the multiprocessing module allows for the creation of separate processes that can run concurrently on different cores of a computer. One of the ways to communicate between these processes is by using queues. The multiprocessing module provides two types of queues: The Queue class is a simple way to create a queue that can be used by mult
10 min read
What is Priority Queue | Introduction to Priority Queue
A priority queue is a type of queue that arranges elements based on their priority values. Elements with higher priority values are typically retrieved before elements with lower priority values. In a priority queue, each element has a priority value associated with it. When you add an element to the queue, it is inserted in a position based on its
15+ min read
Practice Tags :