You are currently viewing Python Deque Methods

In Python we have several deque methods, and using these methods we can implement deques very efficiently. The deque stands for Double Ended Queue which is a one-dimensional data structure like a list in python. In this data structure, we can perform insertion and deletion operations from both ends.

Advertisements

In this article, we will discuss different methods available in a deque data structure in Python and learn how we can append, rotate, reverse, extend, count the items, and remove the items from both sides.

1. Deque Methods in Python

Following are the methods of Deque.

Deque MethodsDescription
append(item)Adds an item to the right
appendleft(item)Adds an item to the left
extend([items])Add multiple items to the right
extendleft([items])Add multiple items to the right
pop()Removes the last item
popleft()Removes the first item
insert(index_pos,item)Item is inserted at the specified index position
len(deque)Return the total number of elements present in the deque
deque.reverse()Reverse the elements in the deque
rotate(value)Rotates particular elements in the deque to the left/right
count(item)Return the total occurrences of an item
index(item,start,end)Return the first occurrence index position of an item.

2. Python Deque append() Method

The append() method will add only a single item to the deque. It takes an item as a parameter and is inserted at the last position.

2.1 Syntax

Following is the syntax of the append() method


# Here, deque1 is the input deque.
# Add single item to the right
deque1.append(item)

2.2 Deque append() Example

Let’s create a deque with 5 countries and add an item to the right of the deque using append() method.


from collections import deque

# Declare deque with 5 countries
deque = deque(['China', 'Russia', 'England', 'Nepal', 'Japan'])
print("Deque: ",deque)

# Using append() to append the item
deque.append('Norway')
print("Deque: ",deque)

Yields below output.

Python deque methods

We have added ‘Norway’ to the deque and it is added at the last (right). Now there are 6 countries in the deque.

3. Python Deque appendleft() Method

The appendleft() will add only a single item to the deque. It takes an item as a parameter. Item is inserted at the first position.

3.1 Syntax

Following is the syntax of the appendleft() method


# Here, deque1 is the input deque.
# Add single item to the right
deque1.appendleft(item)

3.2 Example

Let’s create a deque with 5 countries and add an item to the left of the deque using appendleft() method.


# Using appendleft() to append the item left of deque
deque.appendleft('Norway')
print("Deque: ",deque)

Yields below output.

Python deque methods

We have added ‘Norway’ to the deque and it is added at the first (left). Now there are 6 countries in the deque.

4. Python Deque extend() Method

extend() adds single or multiple items to the deque . It takes a list of items as a parameter. Items are inserted at the last position one by one.

4.1 Syntax

Following is the syntax of the extend() method


# Here, deque1 is the input deque.
deque1.extend(item)

4.2 Example

Let’s create deque with 5 countries and add 2 items to the right of the deque using the extend() method.


from collections import deque

# Declare deque with 5 countries
deque = deque(['China','Russia','England','Nepal','Japan'])
print("Deque: ",deque)

# Using extend() to extend the deque
deque.extend(['Norway','Italy'])
print("Deque: ",deque)

# Output:
# Deque:  deque(['China', 'Russia', 'England', 'Nepal', 'Japan'])
# Deque:  deque(['China', 'Russia', 'England', 'Nepal', 'Japan', 'Norway', 'Italy'])

‘Norway’ and ‘Italy’ were added to the deque one after the other.

5. Python Deque extendleft() Method

extendleft() adds single or multiple items to the deque . It takes list of items as a parameter. Items are inserted at the first position (The last element is added first).

5.1 Syntax

Following is the syntax of the extendleft() method


# Here, deque1 is the input deque.
deque1.extendleft(item)

5.2 Example

Let’s create a deque with 5 countries and add 2 items to the left of the deque using the extendleft() method.


from collections import deque

# Declare deque with 5 countries
deque = deque(['China','Russia','England','Nepal','Japan'])
print("Deque: ",deque)

# Using extendleft() to extend the deque from left
deque.extendleft(['Norway','Italy'])
print("Deque: ",deque)

# Output:
# Deque:  deque(['China', 'Russia', 'England', 'Nepal', 'Japan'])
# Deque:  deque(['Italy', 'Norway', 'China', 'Russia', 'England', 'Nepal', 'Japan'])

‘Norway’ and ‘Italy’ were inserted at left in the deque.

6. Python Deque pop() Method

pop() method removes the last element present in the deque and returns the popped item. It takes no parameter.

If the deque is empty. It will return an “IndexError – pop from an empty deque“. This scenario is handled with try-except block.

6.1 Syntax

Following is the syntax of the pop() method


# Here, deque1 is the input deque.
deque1.pop()

6.2 Example

Let’s create deque with 5 countries and use pop() method to remove last item from the deque.


from collections import deque

# Declare deque with 5 countries
deque = deque(['China','Russia','England','Nepal','Japan'])

# Using pop() to remove last item
print("Popped item: ",deque.pop())
print("Deque: ",deque)

# Output:
# Popped item:  Japan
# Deque:  deque(['China', 'Russia', 'England', 'Nepal'])

‘Japan’ is removed from the deque since it is the last item.

7. Python Deque popleft() Method

popleft() removes the first element present in the deque and returns the popped item. It takes no parameter.

If the deque is empty. It will return an “IndexError – pop from an empty deque“. This scenario is handled with try-except block.

7.1 Syntax

Following is the syntax of the popleft() method


# Here, deque1 is the input deque.
deque1.popleft()

7.2 Example

Let’s create a deque with 5 countries and use popleft() method to remove the first item from the deque.


from collections import deque

# Declare deque with 5 countries
deque = deque(['China','Russia','England','Nepal','Japan'])

# Using popleft() to remove first item
print("Popped item: ",deque.popleft())
print("Deque: ",deque)

# Output:
# Popped item:  China
# Deque:  deque(['Russia', 'England', 'Nepal', 'Japan'])

‘China’ is removed from the deque since it is the first item.

8. Python Deque insert() Method

insert() will add an item at the specified position. By using the index position, an item is inserted.

It will take index_pos as the first parameter that specifies the index position and the item to be inserted as the second parameter.

8.1 Syntax

Following is the syntax of the insert() method


# Here, deque1 is the input deque.
deque1.insert(index_pos,item)

8.2 Example

Let’s create a deque with 5 countries and insert – ‘New city’ at 2nd index position.


from collections import deque

# Declare deque with 5 countries
deque = deque(['China','Russia','England','Nepal','Japan'])
print("Deque: ",deque)

# Using insert() to insert an item at specified position
deque.insert(2,'New city')
print("Deque: ",deque)

# Output:
# Deque:  deque(['China', 'Russia', 'England', 'Nepal', 'Japan'])
# Deque:  deque(['China', 'Russia', 'New city', 'England', 'Nepal', 'Japan'])

We can see that ‘New city’ is inserted at 2nd index position.

9. Python Deque count() Method

count() will return the total number of occurrences if specified item. It will take item as a parameter.

9.1 Syntax

Following is the syntax of the count() method


# Here, deque1 is the input deque.
deque1.count(item)

9.2 Example

Let’s create deque with 5 countries and return the total occurrences of ‘England’.


from collections import deque

# Declare deque with 5 countries
deque = deque(['England','England','England','England','Japan'])
print("Deque: ",deque)

# Using count() to count the specified item
print(deque.count('England'))

# Output:
# Deque:  deque(['England', 'England', 'England', 'England', 'Japan'])
# 4

‘England’ occurred 4 times in the deque.

10. Python Deque len() Method

len() will return the total number of elements present in the deque. It takes deque as an input parameter.

10.1 Syntax

Following is the syntax of the len() method


# Here, deque1 is the input deque.
len(deque1)

10.2 Example

Let’s create deque with 5 countries and return the length of it.


from collections import deque

# Declare deque with 5 countries
deque = deque(['England','Japan','China','England','England'])
print("Deque: ",deque)

# Using len() to get the number of item
print(len(deque))

# Output:
# Deque:  deque(['England', 'Japan', 'China', 'England', 'England'])
# 5

Total number of elements is 5.

11. Python Deque reverse() Method

reverse() is used to reverse the entire deque. It takes no parameter. It won’t return anything. If we display the deque after applying the reverse() function, we can see the elements are returned in the reverse order.

11.1 Syntax

Following is the syntax of the reverse() method


# Here, deque1 is the input deque.
deque1.reverse()

11.2 Example

Let’s create deque with 5 countries and reverse it.


from collections import deque

# Declare deque with 5 countries
deque = deque(['China','Russia','England','Nepal','Japan'])
print("Deque: ",deque)

# Using reverse() to reverse the deque
deque.reverse()
print("Deque: ",deque)

# Output:
# Deque:  deque(['China', 'Russia', 'England', 'Nepal', 'Japan'])
# Deque:  deque(['Japan', 'Nepal', 'England', 'Russia', 'China'])

We can see that elements in the deque are reversed.

12. Python Deque rotate() Method

rotate() is used to shift the elements within the deque. It takes an integer which specifies the number of elements to be rotated.

If the integer is positive, elements are rotated to right, Otherwise elements will be rotated to left.

12.1 Syntax

Following is the syntax of the rotate() method


# Here, deque1 is the input deque.
deque1.rotate(value)

12.2 Examples

Example 1: Let’s create deque with 5 countries and rotates by 2 to right.


from collections import deque

# Declare deque with 5 countries
deque = deque(['China','Russia','England','Nepal','Japan'])
print("Deque: ",deque)

# Using rotate() to rotate the deque
deque.rotate(2)
print("Deque: ",deque)

# Output:
# Deque:  deque(['China', 'Russia', 'England', 'Nepal', 'Japan'])
# Deque:  deque(['Nepal', 'Japan', 'China', 'Russia', 'England'])

Example 2: Let’s create deque with 5 countries and rotates by 2 to left.


from collections import deque

# Declare deque with 5 countries
deque = deque(['China','Russia','England','Nepal','Japan'])
print("Deque: ",deque)

# rotate()
deque.rotate(-2)
print("Deque: ",deque)

# Output:
# Deque:  deque(['China', 'Russia', 'England', 'Nepal', 'Japan'])
# Deque:  deque(['England', 'Nepal', 'Japan', 'China', 'Russia'])

13. Python Deque index() Method

index() method is used to return the index of particular item specified in it. It there are multiple items in the deque, first occurrence index position is returned.

13.1 Syntax

Following is the syntax of the index() method


# Here, deque1 is the input deque.
deque1.index(item,start,end)

13.2 Parameters

It takes three parameters. Seconsdand third parameter is optional.

  1. item to be searched in the deque
  2. start specifies the start position in which the item is searched from this position.
  3. end specifies the stop/end position in which the item is searched till this position.

13.3 Example

Let’s create deque with 5 countries and use index() method to return indices of particular element.


from collections import deque

# Declare deque with 5 countries
deque = deque(['England','Japan','China','England','England'])
print("Deque: ",deque)

# index()
print(deque.index('England',0,4))

# index()
print(deque.index('England',2,4))

# Output:
# Deque:  deque(['England', 'Japan', 'China', 'England', 'England'])
# 0
# 3
  1. From position -0 to 4: First index position of ‘England’ is 0.
  2. From position -2 to 4: First index position of ‘England’ is 3.

14. Conclusion

In this article, we have seen all 12 methods supported by the deque data structure in python. And also learned how we can perform the deque from both sides with examples.