Python Deque Peek

Python Deque Peek

  1. Overview of Python Deque Peek
  2. Solution: Use Indexes to Peek the Front Element via deque Class

This article demonstrates how to view/inspect the elements present at the front of a deque (Double ended queue) in Python without having to remove them from the deque.

Overview of Python Deque Peek

While using a deque in our program, we may want to check what is at the front of our deque. Based on what might be present at the front of the deque, we might want to take a different action.

While popping the element and checking for our necessary condition might seem the most obvious, it is not always the best choice. A typical scenario can be a constraint that does not allow altering the deque; other than that, it is also an unnecessary operation.

Consider the following code:

from collections import deque

data = deque([1,2,3,4,5,6,7,8,9,10])

In this snippet of code, we are first importing the deque data structure from the collections module, which comes with the installation of Python. After which, a deque is initialized, with elements ranging from 1 to 10.

Now, it is required that we access the deque’s front without popping elements of a deque from any direction. How can we do that? Let’s learn it below.

Solution: Use Indexes to Peek the Front Element via deque Class

Before discussing the solution, we must learn how indexing works in Python and how we can use indexes to our advantage to access the front member of the deque without popping any element.

Indexes in Python

The Python index() method refers to specific elements within an iterable depending on their position. Therefore, we can also say that we can directly access the desired elements within an iterable and perform various actions based on your requirements.

Like almost all other programming languages, objects in Python are zero-indexed, meaning the position count begins at zero. Numerous additional programming languages follow the same structure.

Therefore, if there are five items in a list, the initial element (the element on the far left) occupies the zero position, followed by the elements in the first, second, third, and fourth positions.

The index() method reveals the index of a specific item within the list if we call it on a list with the item’s name as the argument.

Consider the following code:

cars = ["Suzuki", "Honda", "Chevrolet", "Ford", "Tesla"]

#Printing out the indexes of Suzuki and Tesla
print("Index of Suzuki: ", cars.index("Suzuki"))
print("Index of Tesla: ", cars.index("Tesla"))

Output:

Index of Suzuki: 0
Index of Tesla: 4

Python Index Operators

Square brackets [] represent the index operator in Python. However, the syntax requires that you place a number within the brackets.

Syntax:

IterableObject[index]

Negative Indexes in Python

Until this point, we have always utilized a positive integer within our index operator (the square brackets, []) in the previous examples; negative indexes also exist.

If we are interested in the final few members of a list, or if we want to index the list from the other end, we can frequently utilize negative integers.

Negative indexing describes this method of indexing from the other end. Consider the following code:

cars = ["Suzuki", "Honda", "Chevrolet", "Ford", "Tesla"]

#Printing out the indexes of Suzuki and Tesla
print(cars[-1])
print(cars[-2])

Which gives the following output:

Tesla
Ford

Now that we know how negative indexing works, the solution to our problem becomes crystal clear. Consider the following code:

from collections import deque

data = deque([1,2,3,4,5,6,7,8,9,10])

print("First Element: " + str(data[0]))
print("Second Element: " + str(data[-1]))

Output:

First Element: 1
Second Element: 10

It works because that deque can also be understood as a list in terms of index-based access.

You can peek the front element using deque[0] and peek the last using deque[-1]. It works without elements popping from the left or right and also appears efficient.

Salman Mehmood avatar Salman Mehmood avatar

Hello! I am Salman Bin Mehmood(Baum), a software developer and I help organizations, address complex problems. My expertise lies within back-end, data science and machine learning. I am a lifelong learner, currently working on metaverse, and enrolled in a course building an AI application with python. I love solving problems and developing bug-free software for people. I write content related to python and hot Technologies.

LinkedIn