# Python – Get the Maximum and the Minimum Value in a Deque

In this tutorial, we will look at how to get the maximum and the minimum 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.

Introductory ⭐

Intermediate ⭐⭐⭐

🔎 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.

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 max and the min value in a deque in Python?

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).

You can use the Python built-in `max()` function to get the maximum value and the built-in `min()` function to get the minimum value in a deque.

The following is the syntax –

```# get the max value in the deque, "queue"
max(queue)
# get the min value in the deque, "queue"
min(queue)```

This is similar to how you’d get the max and min values in other iterables (like list, tuple, etc.) in Python.

## Examples

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

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

```from collections import deque

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

Output:

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

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

### Example 1 – Get the maximum value in a deque

Pass the deque to the Python built-in `max()` function.

Let’s get the max value in the deque created above.

```# get the max value in the deque
print(max(queue))```

Output:

`7`

We get the maximum value in the deque, `queue` as `7`.

### Example 2 – Get the minimum value in a deque

Pass the deque to the Python built-in `min()` function.

Let’s get the min value in the same deque used in the example above.

```# get the min value in the deque
print(min(queue))```

Output:

`1`

We get the minimum value in the deque, `queue` as `1`.

You might also be interested in –

• 