How to get the first and last elements of Deque in Python?


A deque is a double ended queue in which insertion and deletion can be performed at both the ends of the queue. It is present in the collections module of Python and has a major use in real life applications. However, since it can perform insertion and deletion from both the ends therefore, it does follow the FIFO (First In First Out) rule.

In this article, we will be discussing how we could get the first and the last element of the deque in Python.

Approaches

  • Approach 1 − In this approach, we will be using the functions like popleft() and pop() to fetch the first and last element of the deque respectively in Python.

  • Approach 2 − In this approach, we will be using the indexes 0 and -1 to fetch the first and last element of the deque respectively in Python.

Fetching the first and last element of the deque using functions

Deques actually makes it very easy to insert or delete elements since they can perform the operations from either ends. There are certain functions in Python by which we can fetch these elements from either side of the deque. One such function is the pop() function, which gives us the last element of the deque. The other such function is the popleft() function in Python which provides us the first element of the deque. Both of these functions are very easy to use and easy to understand.

Syntax

The syntax of the functions is as follows −

deque_name.pop()
deque_name.popleft()

In the above syntax, deque_name will be replaced by the name of the deque used in the program.

Fetching the first and last element of the deque using indexes

You must have dealt with indexes while using arrays in Python. Similar indexes can be used in deques as well to fetch its elements and print the results. Indexing in Python starts with a 0. Therefore, for fetching the first element of the deque, we will be using the index 0 within the square brackets like [0]. However, to access the elements from the end, the indexing starts from a negative 1. Therefore, to access the last element of the deque, we will be using the index -1 within square brackets like [-1].

Syntax

The syntax of the above indexing operation is as follows −

deque_name[0]
deque_name[-1]

In the above syntax, deque_name will be replaced by the name of the deque used in the program.

Algorithm

  • Step 1 − Import and create a deque.

  • Step 2 − Add some elements into the deque.

  • Step 3 − Access the first element of the deque either using the popleft() function or by using the index 0.

  • Step 4 − Now, access the last element of the deque either using the pop() function or by using the index -1.

  • Step 5 − Print the results.

Approach 1

In this approach, we will be creating a deque, adding some elements into it and then use the popleft() and pop() functions to fetch the first and last element of the deque respectively in Python.

Below is the code example for the same.

Example

from collections import deque
q = deque(['Apple','Mango','Grapes', 'Banana', 'Watermelon'])
print("First element of the deque:", q.popleft())
print("Last element of the deque:", q.pop())

Output

First element of the deque: Apple
Last element of the deque: Watermelon

As you can see in the above code example, we have first imported the deque from the collections module of Python, added some string elements into it, and then used the popleft() and pop() functions to get the first and last element of the deque respectively.

Approach 2

In this approach, we will be creating a deque, adding some elements into it and then use the index 0 and -1 to fetch the first and last element of the deque respectively in Python.

Below is the code example for the same.

Example

from collections import deque
q = deque([23, 12, 45, 67, 32])
print("First element of the deque:", q[0])
print("Last element of the deque:", q[-1])

Output

First element of the deque: 23
Last element of the deque: 32

As you can see in the above code example, we have first imported the deque from the collections module of Python, added some integer elements into it, and then used the q[0] and q[-1] indexes to get the first and last element of the deque respectively.

Conclusion

In this article, we have studied about the deques and the ways in which we can fetch the first and last element of the deque in Python. One of them being the use of functions like popleft() and pop() and the other one being the use of indexes 0 and -1 respectively to get the first and the last element of the deque.

Updated on: 24-Jul-2023

592 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements