convert deque to a list in python

Python – Convert a deque to a list

In this tutorial, we will look at how to convert a deque object to a list in Python with the help of some examples.

deque in Python

The collections module in Python comes with a deque class that you can use to implement a stack or queue data structure in Python. Deques support thread-safe, memory efficient appends and pops from either side of the deque with approximately the same O(1) performance in either direction.

In simple terms, you can think of a deque in Python as a list-like data structure but with efficient insertion and removal of items from either end (left and right). This makes them very useful when implementing a stack or queue functionality in Python.

How to convert a deque to a list?

convert deque to a list in python

Deque objects in Python are iterable; thus, you can use the Python list() constructor to directly convert a deque to a list. Pass the deque object as an argument.

The following is the syntax –

# get list from deque, "queue"
list(queue)

It returns a Python list with the values (in the same order) from the deque object

Examples

Let’s now look at some examples of using the above syntax

📚 Data Science Programs By Skill Level

Introductory

Intermediate ⭐⭐⭐

Advanced ⭐⭐⭐⭐⭐

🔎 Find Data Science Programs 👨‍💻 111,889 already enrolled

Disclaimer: Data Science Parichay is reader supported. When you purchase a course through a link on this site, we may earn a small commission at no additional cost to you. Earned commissions help support this website and its team of writers.

Example 1 – Convert a deque to a list

First, let’s create a deque with some elements

from collections import deque

# create a deque
queue = deque([1, 2, 3])
# print the deque
print(queue)

Output:

deque([1, 2, 3])

Here, we imported the deque class from the collections module and created a deque queue with some values.

Let’s now convert the above deque to a list.

# convert deque to list
ls = list(queue)
print(ls)

Output:

[1, 2, 3]

Here, we used the list() constructor to create a list with values from the deque queue. You can see that the resulting list contains the same values as the deque.

Example 2 – Convert a bounded deque to a list

You can similarly convert a bounded deque (a deque with a specified maximum size) to a list.

Let’s create a bounded deque.

# create a bounded deque
queue = deque([1, 2, 3], maxlen=4)
# print the deque
print(queue)

Output:

deque([1, 2, 3], maxlen=4)

Here, we created a bounded deque, queue with three elements and maxlen as 4.

Let’s now create a list from this bounded deque.

# convert deque to list
ls = list(queue)
print(ls)

Output:

[1, 2, 3]

You can see that the resulting list contains the same values as the deque. Also, note that the list does not contain any information about the maximum size (which is a property of a deque object and not a list object).

You might also be interested in –


Subscribe to our newsletter for more informative guides and tutorials.
We do not spam and you can opt out any time.


Author

  • Piyush Raj

    Piyush is a data professional passionate about using data to understand things better and make informed decisions. He has experience working as a Data Scientist in the consulting domain and holds an engineering degree from IIT Roorkee. His hobbies include watching cricket, reading, and working on side projects.

Scroll to Top