Dumping Queue Into List or Array in Python

Dhruv Sharma
Last Updated: May 13, 2022

Introduction 

 

Queues are one of the crucial linear types of data structures that have ample applications in various areas of computer science and the many real-world scenarios and examples that it is seen in, such as booking tickets in ticket lines/queues, placing requests to pre-buy products online etc. In Python, Queues can either be implemented or directly be used through different available libraries such as collections and queues.

 

In this article, we will see various techniques to dump/convert the items stored in a queue in the form of a list or array in Python. 

 

Source

 

Approach 1 
 

Dequeuing Elements and Appending them back in a list/array

 

One can use the deque method offered in queues to dump all the items present in a list or an array. This can be done by dequeuing all elements from a queue and appending them into a list or an array.

 

One can do this in the following manner:

 

Code:

 

# Approach 1 for Dumping queue into a list


from collections import deque
from queue import Queue


# Initializing a queue
q = deque()


# Adding elements to a queue
q.append('a')
q.append('b')
q.append('c')


# display the queue
print("Queue using collections.deque: ")
print(q)
# display the type
print(type(q),"\n")


# converting into list
li1 = []


while len(q) > 0:
   li1.append(q.popleft())
 
# display
print("First Converted list: ")
print(li1)
print(type(li1), '\n')


que = Queue()


que.put(10)
que.put(9)
que.put(6)
que.put(8)


# display the queue
print("Queue using queue.Queue: ")
print(que.queue)
# display the type
print(type(que),"\n")


# converting into list
li2 = []


while que.qsize() > 0:
   li2.append(que.get())
 
# display
print("First Converted list: ")
print(li2)
print(type(li2), '\n')




Output:


Queue using collections.deque: 
deque(['a', 'b', 'c'])
<class 'collections.deque'> 


First Converted list: 
['a', 'b', 'c']
<class 'list'> 


Queue using queue.Queue: 
deque([10, 9, 6, 8])
<class 'queue.Queue'> 


First Converted list: 
[10, 9, 6, 8]
<class 'list'>

 

Explanation

As you can see here, we were able to dump the elements in the queue in a list/array using the deque methods that the various library implementations of queues in Python.

 

First, we saw how dumping elements from collections.deque implementation of queues in Python can be done using the ‘popleft()’ method present in the library.

 

While for queue.Queue implementation of queues in python one can use ‘get()’ to deque the elements from the queue to a list/array quickly.

 

Time Complexity: O(n), traversing the queue to traverse the items and append them in a list/array.

Space Complexity: O(1) as no extra space was required in the intermediate dequeuing steps.

 

Approach 2 

 

Dequeuing Elements and casting/transforming them directly to a list/array

 

One can use the deque method offered in queues to dump all the items present in a list or an array. This can be done by dequeuing all elements from a queue and appending them into a list or an array.

 

One can do this in the following manner:

 

Code:

 

# Approach 2 for Dumping queue into a list


from collections import deque
from queue import Queue


# Initializing a queue
q = deque()


# Adding elements to a queue
q.append('a')
q.append('b')
q.append('c')


# display the queue
print("Queue using collections.deque: ")
print(q)
# display the type
print(type(q),"\n")


# converting into list
li1 = list(q)
 
# display
print("First Converted list: ")
print(li1)
print(type(li1), '\n')


que = Queue()


que.put(10)
que.put(9)
que.put(6)
que.put(8)


# display the queue
print("Queue using queue.Queue: ")
print(que.queue)
# display the type
print(type(que),"\n")


# converting into list
li2 = list(que.queue)


# display
print("First Converted list: ")
print(li2)
print(type(li2), '\n')

 

Output:

 

Queue using collections.deque: 

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

<class 'collections.deque'> 

 

First Converted list: 

['a', 'b', 'c']

<class 'list'> 

 

Queue using queue.Queue: 

deque([10, 9, 6, 8])

<class 'queue.Queue'> 

 

First Converted list: 

[10, 9, 6, 8]

<class 'list'>

 

Explanation

As you can see here, we could dump the elements in the queue in a list/array by directly casting/converting them into a list/array in Python.

 

First, we saw a collections.deque implementation of a queue can be cast/converted directly into a list/array in Python.

 

While for queue.Queue implementation of queues in Python, one can use ‘queue()’ method to directly cast/convert the items in the queue to a list/array.

 

Time Complexity: O(n), as the explicit conversions of elements in a queue directly into list/array takes O(n) time.

Space Complexity: O(1) as no extra space was required in the intermediate casting/conversion steps.

 

FAQ'S

 

1). How can queues be implemented in Python?

Queues can be implemented either by implementing various functionalities of a queue such as enqueue(), dequeue(), front(), back() etc. or by directly using libraries such as collections.deque or queue.Queue in Python.

 

2). What are the various methods and properties that a queue has?

A queue is a FIFO (first-in-first-out) form of linear data structure that has two ends from which the elements are inserted and removed. The elements in a queue are processed so that they can be inserted (enqueued) from the front but can only be removed (dequeued) from the rear end of the queue. A queue has the following set of properties:

enqueue() - operation for inserting elements from the front of a queue, Time Complexity O(1),

dequeue() - operation to remove elements from the rear (back) of a queue, Time Complexity O(1),

front() - operation to get the element from the front of a queue, Time Complexity O(1),

rear() - operation to get the element from the back of a queue, Time Complexity O(1),

size() - operation to get the size of elements in the queue, Time Complexity O(N),

isEmpty() - operation to check whether queue is empty, Time Complexity O(1),

printQueue() - operation to print the elements in the queue, Time Complexity O(N),

 

Key Takeaways

 

In this article, we looked at a few implementations of queues in Python using the collections and queue libraries and how the elements in the queue can be dumped in lists/arrays using different approaches.

This can be a valuable trick for someone to easily convert a queue to a list or an array in Python. If you would like to practice more interview questions, you can head over to CodeStudio and brush up on your skills for top product-based companies on topics such as queues.

 

To gain more information regarding interview preparation, do visit CodeStudio.

 

Keep Coding!!!

 

Was this article helpful ?
0 upvotes

Comments

No comments yet

Be the first to share what you think