You are currently viewing How to Delete Item from Deque in python

Python provides various ways to delete the items from the deque. 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

Related: Python Deque Methods

In this article, we will see how to delete the items from deque with the following methods.

  1. Remove the last item from the deque using pop()
  2. Remove the first item from the deque using popleft()
  3. Remove particular item from deque using remove()
  4. Remove all items from the deque using clear()
  5. Delete deque using the del keyword.

It is important to import deque from the collections module.

1. Quick Examples of Delete Items from Deque

Following are quick examples of deleting items from deque in python.


# Below are some quick examples.

# Example 1: Using pop() to remove last element
popped=deque1.pop()
print("Removed item: ",popped)
print("Final: ",deque1)

# Example 2:  Using popleft() to remove first element
popped=deque1.popleft()
print("Removed item: ",popped)
print("Final: ",deque1)

# Example 3: Using remove() to remove particular element
deque1.remove('England')
print("Final: ",deque1)

# Example 4: Using remove() to remove 'Spain' with try & except
try: 
 deque1.remove('Spain')
 print("Final: ",deque1)
except:
 print("Not exists")

# Example 5: Using clear() to remove all elements
deque1.clear()
print("Final: ",deque1)

# Example 6:  Using del keyword to delete deque
del deque1
try:
 if deque1:
  print("Exists")
except:
  print("Not exists")

2. Delete Last Item from Deque using pop()

The pop() method will remove the last item from the deque. It won’t take any parameters.

pop() method will return the popped item. If we display the deque after applying the pop() method, the popped element won’t exist in the deque.

Let’s create a deque with 6 countries and remove the last item using the pop() method.


# Import
import collections
	
# Declaring deque with 6 countries
deque1 = collections.deque(['China','Russia','Italy','England','UK','USA'])
print("Actual: ",deque1)

# Using pop() to remove last element
popped=deque1.pop()
print("Removed item: ",popped)
print("Final: ",deque1)

This yields the below output.

Python delete item deque

We can see that pop() method returned ‘USA’ which is the last item and it is removed from the deque.

3. Delete First Item from Deque using popleft()

The popleft() method will remove the first item from the deque. It won’t take any parameters. popleft() method will return the popped item. If we display the deque after applying the popleft() method, the popped element won’t exists in the deque.

Let’s create a deque with 6 countries and remove the first item using the popleft() method.


import collections
	
# Declaring deque with 6 countries
deque1 = collections.deque(['China','Russia','Italy','England','UK','USA'])
print("Actual: ",deque1)

# Using popleft() to remove first element
popped=deque1.popleft()
print("Removed item: ",popped)
print("Final: ",deque1)

# Output:
# Actual:  deque(['China', 'Russia', 'Italy', 'England', 'UK', 'USA'])
# Removed item:  China
# Final:  deque(['Russia', 'Italy', 'England', 'UK', 'USA'])

We can see that popleft() method returned ‘China’ which is the first item and it is deleted from deque.

4. Delete Item from Deque using remove()

remove() method will delete a particular item specified in it. It will take the element to be removed as a parameter. If the element is not found, it will return “ValueError”. It can be handled using try-except block.

4.1 Examples

Example 1: Let’s create a deque with 6 countries and remove a particular item from the deque.


import collections
	
# Declaring deque with 6 countries
deque1 = collections.deque(['China','Russia','Italy','England','UK','USA'])
print("Actual: ",deque1)

# Using remove() to remove particular element
deque1.remove('England')
print("Final: ",deque1)

# Output:
# Actual:  deque(['China', 'Russia', 'Italy', 'England', 'UK', 'USA'])
# Final:  deque(['China', 'Russia', 'Italy', 'UK', 'USA'])

‘England’ exists in the deque, So it is removed.

Example 2: Let’s try to remove the item which doesn’t exist in the deque.


import collections
	
# Declaring deque with 6 countries
deque1 = collections.deque(['China','Russia','Italy','England','UK','USA'])
print("Actual: ",deque1)

# Using remove() to remove 'Spain' with try & except
try: 
 deque1.remove('Spain')
 print("Final: ",deque1)
except:
 print("Not exists")
 
# Output:
# Actual:  deque(['China', 'Russia', 'Italy', 'England', 'UK', 'USA'])
# Not exists

‘Spain’ doesn’t exist in the deque, So we handled it inside the except block.

5. Delete All Items from Deque Using clear()

clear() method removed all items from deque. If we display the deque after applying this method, it will return an empty deque.

Let’s create a deque with 6 countries and remove all elements using the clear() method.


import collections
	
# Declaring deque with 6 countries
deque1 = collections.deque(['China','Russia','Italy','England','UK','USA'])
print("Actual: ",deque1)

# Using clear() to remove all elements
deque1.clear()
print("Final: ",deque1)

# Output:
# Actual:  deque(['China', 'Russia', 'Italy', 'England', 'UK', 'USA'])
# Final:  deque([])

All countries are removed from deque since it returned the empty deque.

6. Delete deque using del keyword

del keyword is used to delete the deque from the memory. If we try to print the deque after deleting it, it will return the 'NameError'. This can be handled with try-except block.

Let’s create a deque with 6 countries and delete it.


import collections
	
# Declaring deque with 6 countries
deque1 = collections.deque(['China','Russia','Italy','England','UK','USA'])
print("Actual: ",deque1)

# Using del keyword to delete deque
del deque1

try:
 if deque1:
  print("Exists")
except:
  print("Not exists")
  
# Output:
# Actual:  deque(['China', 'Russia', 'Italy', 'England', 'UK', 'USA'])
# Not exists

7. Conclusion

In this article, we have seen how to delete item/items from deque in python. If you want to delete the first item, you can use the popleft() method. If you want to delete the last item, you can use the pop() method. The desired item from the deque is removed using the remove() method. We utilized the clear() method to remove all items from the deque and the del keyword to remove the entre deque from the memory.