How to get the first and last elements of Deque in Python? Last Updated : 01 Oct, 2020 Summarize Comments Improve Suggest changes Share Like Article Like Report Deque is a Double Ended Queue which is implemented using the collections module in Python. Let us see how can we get the first and the last value in a Deque. Method 1: Accessing the elements by their index. The deque data structure from the collections module does not have a peek method, but similar results can be achieved by fetching the elements with square brackets. The first element can be accessed using [0] and the last element can be accessed using [-1]. Python3 # importing the module from collections import deque # creating a deque dq = deque(['Geeks','for','Geeks', 'is', 'good']) # displaying the deque print(dq) # fetching the first element print(dq[0]) # fetching the last element print(dq[-1]) Output: deque(['Geeks', 'for', 'Geeks', 'is', 'good']) Geeks good Method 2: Using the popleft() and pop() method popleft() method is used to pop the first element or the element from the left side of the queue and the pop() method to pop the last element or the element form the right side of the queue. It should be noted that these methods also delete the elements from the deque, so they should not be preferred when only fetching the first and the last element is the objective. Python3 # importing the module from collections import deque # creating a deque dq = deque(['Geeks','for','Geeks', 'is', 'good']) # displaying the deque print(dq) # fetching and deleting the first element print(dq.popleft()) # fetching and deleting the last element print(dq.pop()) # displaying the deque print(dq) Output: deque(['Geeks', 'for', 'Geeks', 'is', 'good']) Geeks good deque(['for', 'Geeks', 'is']) Comment More infoAdvertise with us Next Article How to get the first and last elements of Deque in Python? Y Yash_R Follow Improve Article Tags : Python deque Python collections-module Practice Tags : Dequepython Similar Reads How toGet First and Last Elements from a Tuple in Python Tuples are immutable sequences in Python that can store a collection of items. Often, we might need to the retrieve the first and last elements from the tuple. In this article, we'll explore how to achieve this using the various methods in Python. Get First and Last Elements from a Tuple Using Index 2 min read Remove first element from list in Python The task of removing the first element from a list in Python involves modifying the original list by either deleting, popping, or slicing the first element. Each method provides a different approach to achieving this. For example, given a list a = [1, 2, 3, 4], removing the first element results in 2 min read How to Get First N Items from a List in Python Accessing elements in a list has many types and variations. For example, consider a list "l = [1,2,3,4,5,6,7]" and N=3, for the given N, our required result is = [1,2,3].This article discusses several ways to fetch the first N elements of a given list.Method 1: Using List SlicingThis problem can be 3 min read How to access the last element in a Pandas series? Prerequisite: Pandas Pandas series is useful in handling various analytical operations independently or as being a part of pandas data frame. So it is important for us to know how various operations are performed in pandas series. The following article discusses various ways in which last element o 3 min read Remove Last Element from List in Python Given a list, the task is to remove the last element present in the list. For Example, given a input list [1, 2, 3, 4, 5] then output should be [1, 2, 3, 4]. Different methods to remove last element from a list in python are:Using pop() methodUsing Slicing TechniqueUsing del OperatorUsing Unpacking 2 min read Like