In this tutorial, we will look at how to remove an element by value from a deque in Python with the help of some examples.
deque
in Python
The collections
module in Python comes with a deque
class that you can use to implement a stack or queue data structure in Python. Deques support thread-safe, memory efficient appends and pops from either side of the deque with approximately the same O(1) performance in either direction.
In simple terms, you can think of a deque
in Python as a list-like data structure but with efficient insertion and removal of items from either end (left and right). This makes them very useful when implementing a stack or queue functionality in Python.
How to remove an element by value from a deque in Python?
The deque
class comes with a number of built-in functions to help perform common operations (like adding and removing elements).
To remove an element by value from a deque in Python, use the deque remove()
function. Pass the element you want to remove as an argument. The following is the syntax –
# remove element "e" from deque "queue" queue.remove(e)
It removes the first occurrence of the element e
in the deque.
Examples
Let’s now look at some examples of using the above syntax –
Example 1 – Remove an element by value from a deque
First, let’s create a deque with some elements.
Introductory ⭐
- Harvard University Data Science: Learn R Basics for Data Science
- Standford University Data Science: Introduction to Machine Learning
- UC Davis Data Science: Learn SQL Basics for Data Science
- IBM Data Science: Professional Certificate in Data Science
- IBM Data Analysis: Professional Certificate in Data Analytics
- Google Data Analysis: Professional Certificate in Data Analytics
- IBM Data Science: Professional Certificate in Python Data Science
- IBM Data Engineering Fundamentals: Python Basics for Data Science
Intermediate ⭐⭐⭐
- Harvard University Learning Python for Data Science: Introduction to Data Science with Python
- Harvard University Computer Science Courses: Using Python for Research
- IBM Python Data Science: Visualizing Data with Python
- DeepLearning.AI Data Science and Machine Learning: Deep Learning Specialization
Advanced ⭐⭐⭐⭐⭐
- UC San Diego Data Science: Python for Data Science
- UC San Diego Data Science: Probability and Statistics in Data Science using Python
- Google Data Analysis: Professional Certificate in Advanced Data Analytics
- MIT Statistics and Data Science: Machine Learning with Python - from Linear Models to Deep Learning
- MIT Statistics and Data Science: MicroMasters® Program in Statistics and Data Science
🔎 Find Data Science Programs 👨💻 111,889 already enrolled
Disclaimer: Data Science Parichay is reader supported. When you purchase a course through a link on this site, we may earn a small commission at no additional cost to you. Earned commissions help support this website and its team of writers.
from collections import deque # create a deque queue = deque([1, 2, 3]) # print the deque print(queue)
Output:
deque([1, 2, 3])
Here, we imported the deque
class from the collections
module and created a deque object, queue
with three elements – 1, 2, and 3.
Now, let’s now remove the element 2
from the deque created above.
# remove element 2 from deque queue.remove(2) # print the deque print(queue)
Output:
deque([1, 3])
Here, we used the deque remove()
function to remove all the element 2
from the deque queue
.
What happens if there are multiple elements with the same value (that you want to delete)?
In that case, the deque remove()
function removes the first occurrence of the value inside the deque.
Let’s look at an example.
# create a deque queue = deque([1, 2, 3, 2, 4]) # remove element 2 from deque queue.remove(2) # print the deque print(queue)
Output:
deque([1, 3, 2, 4])
Here, the deque queue
contains the element 2
at two different indices. When we use the deque remove()
function and pass 2
, the first occurrence of the element is removed from the deque.
To remove all the occurrences of an element from a deque, use the remove()
inside a while
loop like below.
# create a deque queue = deque([1, 2, 3, 2, 4]) # remove all occurrences of a value from deque val = 2 while val in queue: queue.remove(val) # print the deque print(queue)
Output:
deque([1, 3, 4])
All the occurrences of the element 2
is removed.
Example 2 – Remove an element not present in the deque
Let’s now try to remove an element that is not present in a deque.
# create a deque queue = deque([1, 2, 3]) # remove element 4 from deque queue.remove(4) # print the deque print(queue)
Output:
--------------------------------------------------------------------------- ValueError Traceback (most recent call last) Input In [7], in <module> 2 queue = deque([1, 2, 3]) 3 # remove element 4 from deque ----> 4 queue.remove(4) 5 # print the deque 6 print(queue) ValueError: deque.remove(x): x not in deque
Here, we created a deque with elements 1, 2, and 3 and then used the remove()
function to remove the element 4 (which is not present in the deque). Since the element is not in the deque, we get a ValueError
.
You might also be interested in –
- Python – Convert a deque of strings to a single string
- Python – Convert a deque to a list
- Python – Get the Index of the Maximum Value in Deque
- Python – Get the Index of the Minimum Value in Deque
- Python – Get the Maximum and the Minimum Value in a Deque
- Python – Check If a Deque is Empty
- Python – Get Value by Index in Deque
- Python – Count occurrences of a value in deque
- Python – Create a deque copy
- Python – Rotate a Deque to the Right and Left
- Python – Extend deque to the left
- Python – Extend deque to the right
- Python – Get the Max Size of a Deque
- Python – Reverse a deque
- Python – Get Index of a Value in Deque
- Python – Remove all elements from a deque (clear deque)
- Python – Append Element to a Deque
- Python – Append Element to the Left in Deque
- Python – Pop Element From Left in Deque
- Python – Pop Element From the End in Deque
- Python – Insert Element at a given Index in Deque
Subscribe to our newsletter for more informative guides and tutorials.
We do not spam and you can opt out any time.