Skip to Content

Python – Get the Index of the Maximum Value in Deque

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

index of the maximum value in a deque

Deques in Python make it very easy to add and remove elements from either end (in approximately O(1) time complexity) but getting the maximum or the minimum value in a deque takes O(N) (assuming you’re using linear search).

To get the index of the maximum value in a deque in Python,

  1. Find the max value in the deque using the Python built-in max() function.
  2. Get the index of the max value from above using the deque index() function.

The following is the syntax –

# get the index of the max value in deque, "queue"
queue.index(max(queue))

If the deque contains the max value at multiple indices, the index of the first occurrence of the max value will be returned (by default).

Examples

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

Example 1 – Get the index of the max value in a deque

First, let’s create a deque with some elements.

from collections import deque

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

Output:

deque([5, 2, 7, 3, 1, 4])

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

Let’s get the index of the maximum value in the deque created above.

For this, first, find the max value in the deque using the max() function and pass it as an argument to the deque index() function to get its index.

# get the index of the max value in the deque
print(queue.index(max(queue)))

Output:

2

We get the index of max value in the deque, queue as 2 which is the correct answer.

Example 2 – Get the index of the max value in a deque with duplicates

What if the maximum value is present at multiple indices inside the deque?

In that case, the deque index() function will return the index of the first occurrence of the value inside the deque (by default). For more on the deque index() function, check this tutorial.

Let’s look at an example.

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

Output:

deque([7, 2, 7, 3, 1, 4])

You can see that in the deque created above, the max value, 7 occurs at indices 0 and 2.

Let’s see what we get using the above syntax.

# get the index of the max value in the deque
print(queue.index(max(queue)))

Output:

0

We get the index of the max value as 0 (which is the index of the first matching value).

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. In the past, he's worked as a Data Scientist for ZS and holds an engineering degree from IIT Roorkee. His hobbies include watching cricket, reading, and working on side projects.