Deque in Python
A Deque is a data structure in the Python collection module that allows fast append and pop operations. In programming, we tend to deal with high volumes of data, and data structures are necessary to store that data. Data structures include lists, tuples, sets, dictionaries, deques, etc.
In this article, we will discuss what a Deque is, why you should use it, and the various operations it allows.
What is a deque?
Deque is short for “double-ended queue”. A Deque is a mutable list-like data structure in Python that allows you to add or remove values from both ends of a linear queue. Although deques are quite similar to lists, they are significantly faster in conducting append and pop operations because of their constant time complexities.
Deque operation in python
Syntax
The syntax for using a deque is simple. In the first line, you need to type
[python]from collection import deque[/python]After that, you call the deque function and insert an iterable. For example:
[python]from collections import deque#number = deque ([iterable], maxlen)number = deque ([1, 2, 3, 4, 5, 6, 7, 8, 9], 5)print (number)[/python]
The result:
The deque syntax takes the value of an iterable and its maximum length.
As you can see in the photo, we created a variable number and assigned to it a deque of numbers with a maximum length of five objects. By default, the deque data type has no limit.
Append() and Appendleft() functions
In deques, you can append data on both ends.
[python]from collections import deque#number = deque ([iterable], maxlen)number = deque ([1, 2, 3, 4, 5, 6, 7, 8, 9])#append(parameter)number.append('hey')print (number)[/python]
The result:
The result of the code with the append function.
After adding the append() function to our variable, the string “hey” is added to our deque.
We can also add an iterable to the left end of our variable with our appendleft() function. See the example below:
[python]from collections import deque#number = deque ([iterable], maxlen)number = deque ([1, 2, 3, 4, 5, 6, 7, 8, 9])#appendleft(parameter)number.appendleft('hello')print (number)[/python]
The result:
appendleft() function pushed the string to the front of the deque.
Pop() and Popleft() functions
With the pop() method, you can remove data at the end of the deque, and with the popleft() method, you can remove data from the front of the deque. The pop() method works slightly differently in Deque and does not accept any parameters. Therefore, you cannot remove data randomly.
pop()
[python]from collections import deque#number = deque ([iterable], maxlen)number = deque ([1, 2, 3, 4, 5, 6, 7, 8, 9])
#pop()number.pop()print (number)[/python]
The result:
With the pop function, we removed the last object.
popleft()
[python]from collections import deque#number = deque ([iterable], maxlen)number = deque ([1, 2, 3, 4, 5, 6, 7, 8, 9])#popleft()number.popleft()print (number)[/python]
The result:
With the popleft() function we removed the first object.
Index() function
Deques also allow you to access a value in your deque and return the first occurrence of that value with the index() function. The syntax goes like this:
[python]from collections import deque#number = deque ([iterable], maxlen)number = deque ([1, 2, 3, 4, 5, 6, 7, 8, 3, 4, 9])#index(value, start, stop)print(number.index(2, 0))[/python]
The result:
The index function prints the position of number 2.
Get CodingCampus’s stories in your inbox
Join Medium for free to get updates from this writer.
You must first declare the value you want to find, the position you want your code to start searching from, and where to end the search. However, it is not necessary to define the end of the search because, by default, the index function will search to the end of the iterable.
Extend() and Extendleft() functions
If you need to add multiple characters to your deque, the extend() and extendleft() functions provide that function.
Extend()
[python]from collections import deque#number = deque ([iterable], maxlen)number = deque ([1, 2, 3, 4, 5, 6, 7, 8, 3, 4, 9])
#extend(iterable)number.extend(["hello", "darkness", "my", "old", "friend"])print(number)[/python]
The result:
We added a string to the deque with the extend function.
Extendleft()
[python]from collections import deque#number = deque ([iterable], maxlen)number = deque ([1, 2, 3, 4, 5, 6, 7, 8, 3, 4, 9])
#extendleft(iterable)number.extendleft("hello")print(number)[/python]
The result:
We added the string to the front of the deque with the extendleft function.
Concatenate
You can also join two different deques by concatenation.
See below:
[python]from collections import deque#number = deque ([iterable], maxlen)number = deque ([1, 2, 3, 4, 5, 6, 7, 8, 3, 4, 9])
sentence = deque (['hello', "hey", "hi"])#extendleft(iterable)print(number + sentence)[/python]
The result:
We added two deques with concatenation.
Insert() function
Although the insert() function acts similarly to the extend function, it is more versatile because it allows you to add data from any index of your deque. The insert function syntax is also quite simple. You specify the iterable and the position to which you want to add it. For example:
[python]from collections import deque#number = deque ([iterable], maxlen)number = deque ([1, 2, 3, 4, 5, 6, 7, 8, 3, 4, 9])#insert(index, iterable)
number.insert(3, [1, 2, 3, 4])print(number)[/python]
The result:
With the insert function, we added our data to the position we wanted it to be.
Note: If you are adding a list, tuple, or set to the deque, you cannot switch places between your iterable and your index. You must follow the syntax strictly, otherwise, you will get an error.
Rotate() function
The rotate() function allows a deque function similar to a circular buffer. The rotate function rotates the position of a specified number of items in your deque. A negative number with the rotate function will move the specified number of items left, and a positive number will move the items right.
An example with a positive number:
[python]from collections import deque#number = deque ([iterable], maxlen)number = deque ([1, 2, 3, 4, 5, 6, 7, 8, 3, 4, 9])#rotate(index)
number.rotate(3)print(number)[/python]
The result:
With the rotate function we rotated our deque.
Clear() function
This function allows you to remove all the values from a deque.
Example:
[python]from collections import deque#number = deque ([iterable], maxlen)number = deque ([1, 2, 3, 4, 5, 6, 7, 8, 3, 4, 9])
#clear()number.clear()print(number)[/python]
The result:
With the clear function, we cleared our deque.
As the example shows, adding the clear function to the variable number returns an empty deque.
Remove() function
Remove(value) allows you to delete the first instance of the specified value and will return ValueError if the value is absent in the Deque.
Example:
[python]from collections import deque#number = deque ([iterable], maxlen)number = deque ([1, 2, 3, 4, 5, 6, 7, 8, 3, 4, 9])#remove(value)<strong>number.remove(3)print(number)[/python]
The result:
With the remove function we removed a single data.
Count() function
This count() method allows you to count the number of times an argument occurs in a deque.
Example
[python]from collections import deque#number = deque ([iterable], maxlen)number = deque ([1, 2, 3, 4, 5, 6, 7, 8, 3, 4, 9])
#count(value)print (number.count(3))[/python]
The result:
The count function returned the number of times 3 appeared in our deque.
Why Should You Employ a Deque?
You can add items on both ends of a deque
Deque acts as a double-linked queue and allows adding and removing of data from both ends. Generally, while working with operations that require you to insert or remove data efficiently from both ends of a queue without disrupting data in the middle, a deque is a better option.
A good application of deques is in retrieving and deleting browser history. Newer browsing history is added to the front of the deque, and older history eventually gets deleted.
Deque is memory-efficient
Deque stores data in a linked-list format, meaning each node in the deque is stored in a separate memory, but all the nodes have the address of the node preceding them. This deque feature makes adding and removing items fast when you compare them to a list.









