Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction 
2.
Approach 1 
3.
Approach 2 
4.
Frequently Asked Questions
4.1.
How can queues be implemented in Python?
4.2.
What are the various methods and properties that a queue has?
5.
Conclusion
Last Updated: Mar 27, 2024
Easy

Dumping Queue Into List or Array in Python

Author Dhruv Sharma
0 upvote
Create a resume that lands you SDE interviews at MAANG
Speaker
Anubhav Sinha
SDE-2 @
12 Jun, 2024 @ 01:30 PM
Queue

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 PythonQueues 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

Also see, Arrays

Queue

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'>

You can also practice with the help of Online Python Compiler

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.

Also see, Collections

Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

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.

Read about Application of Queue in Data Structure here.

Must Read Python List Operations

Frequently Asked Questions

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.

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),

Conclusion

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 Coding Ninjas Studio and brush up on your skills for top product-based companies on topics such as queues.

Recommended Readings:


Do check out The Interview guide for Product Based Companies as well as some of the Popular Interview Problems from Top companies like Amazon, Adobe, Google, Uber, Microsoft, etc. on Coding Ninjas Studio.

Also check out some of the Guided Paths on topics such as Data Structure and Algorithms, Competitive Programming, Operating Systems, Computer Networks, DBMS, System Design, Basics of C++, Basics of Java, Basics of Python, etc. as well as some Contests, Test Series, Interview Bundles, and some Interview Experiences curated by top Industry Experts only on Coding Ninjas Studio.

Keep Coding!!!

Previous article
Queue Remove Method in Java
Next article
Find the Index of the Array Elements after Performing given Operations K Times
Live masterclass