How to Peek in Front of a Deque Without Popping?

HOW TO PEEK IN FRONT OF A

A Deque is an abbreviation for the double-ended queue which means that it allows insertion and deletion at both ends of the queue, unlike a queue that follows the First In First Out (FIFO) policy the operations insertion and deletion occur at different ends.

A deque is an extension of a popular data structure – Queue. A Queue follows FIFO order where the first element to be inserted will be the first element to leave just like a real-life queue.

Coming to deque, it is similar to a queue but allows insertion and deletion to occur at both ends.

Refer to this article to know more about Queues in Python.

What Is a Deque?

A deque is a queue that allows deletion and insertion from the front and back. We can access the deque data structure from a Python module called collections.

Here is an image that demonstrates the operations in a deque.

DEQUE
DEQUE

Let us see an example of the creation of a deque.

from collections import deque
deq=deque([20,30,40,50,10])
print("The deque is:",deq)

In the first line, we are importing the deque from the collections module. Next, we are creating a variable called deq to store the deque. Next, we are printing the deque.

Deque
Deque

Let us take a look at the operations on a deque.

from collections import deque
deq=deque([20,30,40,50,10])
print("The deque is:",deq)
deq.append(80)
print("The queue after insertion at the end:",deq)
deq.appendleft(15)
print("The queue after insertion at the front:",deq)
deq.pop()
print("The queue after popping the right most element:",deq)
deq.popleft()
print("The queue after popping the left most element:",deq)

In the first line, we are importing the collection module’s deque data structure.

Next, we are creating a variable called deq to store the deque. We are performing four operations- append, apendleft, pop, popleft.

The append and pop are used to insert and remove the elements from the end of the deque whereas the appendleft and popleft do the same job but at the front of the deque.

Operations On Deque
Operations On Deque

How to Peek in Front of a Deque Without Popping?

Suppose you want to look at the first element at the front. The traditional way to know the first element is to pop the element. Since the pop function returns the topmost element and also deletes it from the deque, it is not the most preferred way. There are other approaches to peek in front of the deque without popping it.

Related: How to know if a deque is empty?

First, let us see what the pop function will do. Since we are talking about the removal of the first element from the front, we should use popleft.

from collections import deque
deq = deque([10, 20, 30, 40, 50])
deq.popleft()
print"The elements of the deque after removal are:",deq)

The popleft function removes the very first element from the front and returns it. In this case, it is 10. Let us also try to check the deque after the removal.

Using Popleft To Return The Front Element
Using Popleft To Return The Front Element

As you can see, the pop function removes the element after returning it. The deque after popping does not contain the topmost element.

There are two approaches to printing the topmost element without popping it.

Using Indexing

We can use indexing to peek in the front of the deque. Let us see how we can do that.

from collections import deque
deq = deque([10, 20, 30, 40, 50])
top_most_element = deq[0]  
print("The top most element is:",top_most_element)  
print("The deque elements are:",deq)

We are importing th collections module and the deque data structure from it. Next, we are initializing a variable called deq to store the elements of the deque. We are creating a variable called top_most_element to store the very first element from the start. Since indexing generally starts with zero in any data structure, we are accessing the element at the zeroth position. Then we are printing the topmost element and check if this action deletes the topmost element from the deque by printing the deque elements.

Peeking In Front Of The Deque By Indexing
Peeking In Front Of The Deque By Indexing

Using Iter and Next

The iter and next functions of Python are used to loop across any item or variable without having to use the for loop. They are mostly used to iterate over data structures like lists, tuples, sets, and so on.

While the iter creates a loop across the object, the next is used to access every item from that object and return it.

Since we are only interested in peeking at the front element, we call the next function just once. We can also use the next function as many times to return all the elements of the object.

from collections import deque
deq = deque([10, 20, 30, 40, 50])
it = iter(deq)
top_most_element = next(it)
print(top_most_element) 
print("The elements of the deque after peeking:\n",deq)

In the third line, we are creating an iterator variable called it using the iter function. The top_most_element stores the very first element returned by the next function. We are printing the topmost element and the entire deque in the last two lines.

Peeking In Front Of The Deque With Iter And Next
Peeking In Front Of The Deque With Iter And Next

Conclusion

To conclude, we have learned the basic definition of the queue. A queue is a data structure that follows the First-in First-out mechanism and allows insertion at the rear end and deletion at the front end. We have also learned the definition of the extension of the queue- a deque. Deque stands for double-ended queue. A deque allows insertion and deletion to occur at both ends.

We have seen an example of a deque and the operations we can perform on the deque. As said above, we can insert and delete elements from both ends. The append and pop functions of the deque insert and delete the elements from the right end while the appendleft and popleft functions do the same job at the left of the deque.

The purpose of this article was to know if there was a way to return the first element in the front without popping it. Usually, we use pop or popleft to return the topmost element because the pop function returns the topmost element. But it also removes the element from the deque. We looked at two methods to return the topmost element from the deque without popping it.

The first method is indexing. Since positioning starts from zero, we can find the topmost element we are looking for in the zeroth position. We can access any element by specifying its index.

The second method we saw uses iter and next functions. The iter function is used to iterate across any iterable data structure and the next function returns the objects in the data structure. Since we only require the first element, we used the next function only once.

References

Deque data structure from the collections module.

Stack Overflow answer chain on the same topic.