# Convert Priority Queue to List & Vice Versa in Python (Examples)

Hi! This tutorial will show you how to turn a priority queue to a list and vice-versa in the Python programming language.

First, though, here is a quick overview:

Letâ€™s dive into Python code!

## Create Sample Priority Queue

Here, we will create the sample priority queue that will be turned into a list in this tutorial. Therefore, in your Python IDE, run the lines of code below to create the sample priority queue:

my_tuple = ((1, "bread"), (3, "pizza"), (5, "steak"), (2, "apple"),(4, "milk"))
my_queue = sorted(my_tuple,reverse=True)

print(my_queue)

# [(5, 'steak'), (4, 'milk'), (3, 'pizza'), (2, 'apple'), (1, 'bread')]

The program has sorted the tuples in the order of priority.

## Example 1: Queue to List | Turn Queue to List Using For Loop

In this first example, we will use a nested for loop to turn the queue into a list:

my_list = []
for i in my_queue:
for j in i:
my_list.append(j)

print(my_list)

# [5, 'steak', 4, 'milk', 3, 'pizza', 2, 'apple', 1, 'bread']

print(type(my_list))

# <class 'list'>

Using a nested for loop, we are able to iterate through the queue and return all its elements appended to my_list. The type() function shows that my_list has the list data type as expected.

## Example 2: Queue to List | Turn Queue to List Using List Comprehension

In this second example, we will use list comprehension to transform the queue to a list:

my_list = [j for i in my_queue for j in i]
print(my_list)

# [5, 'steak', 4, 'milk', 3, 'pizza', 2, 'apple', 1, 'bread']

print(type(my_list))

# <class 'list'>

The list comprehension method does exactly the same thing as the nested for loop method in the previous example, only that it is a more concise approach.

Now that we have examined how to turn a priority queue into a list, let us now consider an example of how to turn the list back into a queue.

## Example: List to Queue | Turn List to Queue Using deque() Function

One of the quickest ways to turn a list into a priority queue is to use the deque() function from the collections module along with a for loop like so:

from collections import deque

# initialize a queue
queue = deque()

# loop through list and append to queue
for i in my_list:
queue.append(i)

print(queue)

# deque([5, 'steak', 4, 'milk', 3, 'pizza', 2, 'apple', 1, 'bread'])

print(type(queue))

# <class 'collections.deque'>

The first thing is to initialize a queue. After the queue has been initialized, we then loop through the list and append its elements to the queue. Printing out the type() shows that it has been turned into a priority queue.

With that, we have demonstrated how to convert a priority queue to a list and vice-versa in Python. I hope you found this tutorial helpful!

## Video, Further Resources & Summary

Do you need more explanations on how to convert a priority queue to a list and vice-versa in Python? Then you should have a look at the following YouTube video of the Statistics Globe YouTube channel.

In the video, we explain in some more detail how to convert a priority queue to a list and vice-versa in Python.

Furthermore, I encourage you to check out other interesting Python list tutorials on Statistics Globe, starting with these ones:

This post has shown how to convert a priority queue to a list and vice-versa in Python. In case you have further questions, you may leave a comment below.

This page was created in collaboration with Ifeanyi Idiaye. You might check out Ifeanyiâ€™s personal author page to read more about his academic background and the other articles he has written for the Statistics Globe website.

Subscribe to the Statistics Globe Newsletter