get value by index in a deque

Python – Get Value by Index in Deque

In this tutorial, we will look at how to get a value in a Python deque by its index 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 get a value at a specific index in a deque in Python?

get value by index in a deque

Deques in Python make it very easy to add and remove elements from either end but you can also use an index to directly access an element inside the deque.

Similar to lists, deques in Python are indexed starting from 0 and you can use the [] notation to get the value at a specific index in a deque.

The following is the syntax –

# get value at index i in deque, "queue"
queue[i]

This is similar to how you’d get the value by its index in other ordered iterables (like list, tuple, string, etc.) in Python.

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 – Get value by index in a deque

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 object, queue with some numbers. You can see that the deque contains three values 1, 2, and 3.

To get a value by using its index in a deque, use its index inside the [] notation. For example, let’s get the value at index 1 in the above deque.

# get the value at index 1
print(queue[1])

Output:

2

We get the value at index 1 in the deque, queue as 2.

If you use an index outside the index range of the deque, you’ll get an IndexError.

# get the value at index 3
print(queue[3])

Output:

---------------------------------------------------------------------------
IndexError                                Traceback (most recent call last)
Input In [11], in <module>
      1 # get the value at index 3
----> 2 print(queue[3])

IndexError: deque index out of range

Here, we’re trying to access the value at the index 3. Since the deque, here, only has values till the index 2, we get an IndexError.

Example 2 – Get value by index in a bounded deque

This method works similarly for bounded deques (deques with a specified maximum size).

For example, let’s create a bounded deque and try to access an element using its index.

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

Output:

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

Here, we created a bounded deque with maxlen 4 and three elements. We then got the value at the index 1 in this bounded deque.

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