We can get the first and last elements of deque using index position, popleft(), and pop() methods in Python. In this article, I will explain how to get first and last element from deque in Python using index position, pop(), and popleft() methods with examples.
deque[0] will return the first element and deque[-1] will return the last element.
popleft() in deque will return the first element and remove it from the deque. With this, we will get the first element.
pop() in deque will return the last element and remove it from the deque. With this, we will get the last element.
1. Quick Examples of Get First and Last Elements from the Deque
Following are quick examples of how to get the first and last element from Deque in python.
# Below are some quick examples.
# Declare deque with 5 countries
deque = deque([‘China’, ‘Russia’, ‘England’, ‘Nepal’, ‘Japan’])
print(“Deque: “,deque)
# Using index-position
print(“First element:”, deque[0])
# Using index-position
print(“Last element:”,deque[-1])
# Using popleft()
print(“First element:”,deque.popleft())
# Using pop()
print(“Last element:”,deque.pop())
2. Get First & Last Element from Python Deque Using Index position
In this scenario, we have the below approaches to get the first element and last elements from Python deque. The first element is present at the index position ‘0’ So, we can get the first element directly from the deque in Python.
Let’s have a deque that holds 5 countries (strings) and get only the first element.
from collections import deque
# Declare deque with 5 countries
deque = deque([‘China’, ‘Russia’, ‘England’, ‘Nepal’, ‘Japan’])
print(“Deque: “,deque)
# Get first element from deque using index-position
print(“First element:”, deque[0])
Yields below output.
The first element present in the deque is ‘China’.
The last element is present at index position ‘-1’ So, we can get the last element directly from the deque in Python.
Example 1: Let’s have a deque that hold 5 countries (strings) and return only the last element by specifying -1 as index.
from collections import deque
# Declare deque with 5 countries
deque = deque([‘China’, ‘Russia’, ‘England’, ‘Nepal’, ‘Japan’])
print(“Deque: “,deque)
# Get last element using index-position
print(“Last element:”, deque[-1])
Yields below output.
The last element present in the deque is ‘Japan’.
Example 2: Let’s have a deque that holds 5 countries (strings) and return only the last element by specifying ‘len(deque)-1’ as index.
from collections import deque
# Declare deque with 5 countries
deque = deque([‘China’,’Russia’,’England’,’Nepal’,’Japan’])
print(“Deque: “,deque)
# Return last element
# Using index-position
print(“Last element:”,deque[len(deque)-1])
# Output:
# Deque: deque([‘China’, ‘Russia’, ‘England’, ‘Nepal’, ‘Japan’])
# Last element: Japan
3. Using popleft() & pop() methods
popleft() will remove the first element from the deque by returning the first element. We can utilize this method to return the first element.
Let’s have a deque that holds 5 countries (strings) and return only the first element using the popleft() method.
from collections import deque
# Declare deque with 5 countries
deque = deque([‘China’,’Russia’,’England’,’Nepal’,’Japan’])
print(“Deque: “,deque)
# Return first element
# Using popleft()
print(“First element:”,deque.popleft())
# Output:
# Deque: deque([‘China’, ‘Russia’, ‘England’, ‘Nepal’, ‘Japan’])
# First element: China
The first element present in the deque is ‘China’.
The pop() will remove the last element from the deque by returning the last element. We can utilize this method to return the last element.
Let’s have a deque that hold 5 countries (strings) and return only the last element using the pop() method.
from collections import deque
# Declare deque with 5 countries
deque = deque([‘China’,’Russia’,’England’,’Nepal’,’Japan’])
print(“Deque: “,deque)
# Return last element
# Using pop()
print(“Last element:”,deque.pop())
# Output:
# Deque: deque([‘China’, ‘Russia’, ‘England’, ‘Nepal’, ‘Japan’])
# Last element: Japan
4. Conclusion
In this article, I have explained by using index position ‘0’, ‘-1’, pop(), and popleft() methods and using these how we can get the first and last element from the deque in Python.
We can get the first and last elements of deque using index position, popleft(), and pop() methods in Python. In this article, I will explain how to get first and last element from deque in Python using index position, pop(), and popleft() methods with examples. 1. Quick Examples of Get First and Last Elements from Read More Python, Python Tutorial, Python Deque