Dumping queue into list or array in Python


A queue is a linear data structure that works on the FIFO property where each element is added to the queue from the rear and the elements are extracted from the front and use the principle of first in first out. In the list, we can access every element while in the queue we can only access the first element. In this tutorial, we are going to see two methods of how to convert a queue into the list.

Creating a Queue in Python

Queue in a linear data structure which works on the first in first out property and to use a queue in Python we have to import deque from the collections package of the python.

Example

# import deque from collections
from collections import deque 

# initialize a deque element 
queue = deque()
# adding elements to the queue 
queue.append(1)
queue.append(2)
queue.append(3)
queue.append(4)
# current queue is 
print(queue)
# popping elements from queue 
queue.popleft()
# printing next element 
print('Current first element of the queue is:', queue.popleft())

Output

deque([1, 2, 3, 4])
Current first element of the queue is: 2

In the queue, adding the element to the rear and removing the first element is done in the O(1) time complexity.

Approach 1: Using Deque

As deque is the supper set of the stack, queue, and vector, we can use it as any of them. Here we will first create a deque and then will use it as the list by casting it to list.

Example

# import deque from collections
from collections import deque 

# initialize a deque element 
queue = deque()

# adding elements to the queue 
queue.append(1)
queue.append(2)
queue.append(3)
queue.append(4)

# current queue is 
print('Current queue is:', queue)

# printing the type of the current data structure 
print(type(queue))

#casting current queue to the list 
print('Converting current queue to list')
lis = list(queue)
print('Current list is: ', lis)

# printing the type of the current data structure 
print(type(lis))

Output

Current queue is: deque([1, 2, 3, 4])
<class 'collections.deque'>
Converting current queue to list
Current list is:  [1, 2, 3, 4]
<class 'list'>

In the above code first, we have created a queue by importing deque from the collections and then casted it to a queue and printed all the required parameters.

The time and space complexity for the above code is linear which is O(N), where N is the total number of elements in the queue.

Approach 2: Using Queue

In this approach, we are going to use the queue data structure from the Queue class of the python and then going to convert that into the list by casting similar to the previous code.

Example

# import Queue from queue
from queue import Queue

# initialize a deque element 
q = Queue()

# adding elements to the queue 
q.put(1)
q.put(2)
q.put(3)
q.put(4)
q.put(5)

# current queue is 
print('Current queue is:', q.queue)

# printing the type of the current data structure 
print(type(q))

#casting current queue to the list 
print('Converting current queue to list')
lis = list(q.queue)
print('Current list is: ', lis)

# printing the type of the current data structure 
print(type(lis))

Output

Current queue is: deque([1, 2, 3, 4, 5])
<class 'queue.Queue'>
Converting current queue to list
Current list is:  [1, 2, 3, 4, 5]
<class 'list'>

The queue data structure is not directly iterable, for that we have to use the queue_name.queue(here queue_name is the name of the queue) to make it iterable for printing as well as to convert to a list. Here again, time and space complexity is O(N).

Conclusion

In this tutorial, we have implemented a program to convert a queue in python to list. To convert the queue to list we have seen two approaches first one is the using deque and another is using the queue by importing them from respective packages. We have to use the casting method to change the queue to list.

Updated on: 24-Aug-2023

115 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements