No Result
View All Result
DevRescue
  • Home
  • Python
  • Lists
  • Movies
  • Finance
  • Opinion
  • About
  • Contact Us
  • Home
  • Python
  • Lists
  • Movies
  • Finance
  • Opinion
  • About
  • Contact Us
DevRescue
Home Blog Python

Python Deque Methods

by Khaleel O.
January 14, 2024
in Python
Reading Time: 5 mins read
A A
python deque methods

In this post we’ll list Python Deque Methods. Ready? Let’s go! 🔥🔥🔥

A deque (double-ended queue) in Python is a type of list where elements can be added to or removed from both ends (front and back) efficiently. It is part of the collections module and is a more generalized form of stack and queue. Deques are thread-safe and can be used to implement both stacks and queues without the performance penalties associated with lists.

Here’s a list of the key methods used on a deque in Python, along with example code and a short description for each of the Python Deque Methods:

1. append(x) – Adds x to the right side of the deque.

from collections import deque
d = deque([1, 2, 3])
d.append(4)  # deque becomes [1, 2, 3, 4]

2. appendleft(x) – Adds x to the left side of the deque.

d.appendleft(0)  # deque becomes [0, 1, 2, 3, 4]

3. pop() – Removes and returns an element from the right side of the deque. Raises IndexError if empty.

d.pop()  # returns 4, deque becomes [0, 1, 2, 3]

4. popleft() – Removes and returns an element from the left side of the deque. Raises IndexError if empty.

d.popleft()  # returns 0, deque becomes [1, 2, 3]

5. extend(iterable) – Adds elements from iterable to the right side of the deque.

d.extend([4, 5])  # deque becomes [1, 2, 3, 4, 5]

6. extendleft(iterable) – Adds elements from iterable to the left side of the deque. Note that the iterable is added in reverse order.

d.extendleft([0, -1])  # deque becomes [-1, 0, 1, 2, 3, 4, 5]

7. rotate(n) – Rotate the deque n steps to the right. If n is negative, rotate to the left.

d.rotate(2)  # deque becomes [4, 5, -1, 0, 1, 2, 3]

8. maxlen – Maximum size of a deque or None if unbounded.

d = deque([1, 2, 3], maxlen=3)  # deque will not grow beyond length 3

9. clear() – Removes all elements from the deque.

d.clear()  # deque becomes empty

10. count(x) – Count the number of deque elements equal to x.

d = deque([1, 2, 3, 2, 4])
d.count(2)  # returns 2

11. index(x, [start, [stop]]) – Return the position of x in the deque (at or after index start and before index stop). Raises ValueError if not found.

d.index(3)  # returns 2

12. insert(i, x) – Insert x into the deque at position i.

d.insert(1, 'a')  # deque becomes [1, 'a', 2, 3, 2, 4]

13. remove(value) – Remove the first occurrence of value. Raises ValueError if not found.

d.remove('a')  # deque becomes [1, 2, 3, 2, 4]

14. reverse() – Reverse the elements of the deque in-place.

d.reverse()  # deque becomes [4, 2, 3, 2, 1]

15.copy() – Creates a shallow copy of the deque.

d_copy = d.copy()  # creates a copy of d

So there you have it, all 15 Python Deque Methods. The full code is as follows:

from collections import deque
d = deque([1, 2, 3])
d.append(4)  # deque becomes [1, 2, 3, 4]
d.appendleft(0)  # deque becomes [0, 1, 2, 3, 4]
d.pop()  # returns 4, deque becomes [0, 1, 2, 3]
d.popleft()  # returns 0, deque becomes [1, 2, 3]
d.extend([4, 5])  # deque becomes [1, 2, 3, 4, 5]
d.extendleft([0, -1])  # deque becomes [-1, 0, 1, 2, 3, 4, 5]
d.rotate(2)  # deque becomes [4, 5, -1, 0, 1, 2, 3]
d = deque([1, 2, 3], maxlen=3)  # deque will not grow beyond length 3
d.clear()  # deque becomes empty
d = deque([1, 2, 3, 2, 4])
d.count(2)  # returns 2
d.index(3)  # returns 2
d.insert(1, 'a')  # deque becomes [1, 'a', 2, 3, 2, 4]
d.remove('a')  # deque becomes [1, 2, 3, 2, 4]
d.reverse()  # deque becomes [4, 2, 3, 2, 1]
d_copy = d.copy()  # creates a copy of d

print(d)
print(d_copy)
Tags: data structuredequepythonqueue
Previous Post

Python Ternary List Comprehension

Next Post

Python Blowfish Encryption Example

Khaleel O.

Khaleel O.

I love to share, educate and help developers. I have 14+ years experience in IT. Currently transitioning from Systems Administration to DevOps. Avid reader, intellectual and dreamer. Enter Freely, Go safely, And leave something of the happiness you bring.

Related Posts

Python

Python Fibonacci Recursive Solution

by Khaleel O.
January 16, 2024
0
0

Let's do a Python Fibonacci Recursive Solution. Let's go! 🔥🔥🔥 The Fibonacci sequence is a series of numbers in which...

Read moreDetails
Python

Python Slice String List Tuple

by Khaleel O.
January 16, 2024
0
0

Let's do a Python Slice string list tuple how-to tutorial. Let's go! 🔥🔥🔥 In Python, a slice is a feature...

Read moreDetails
Python

Python Blowfish Encryption Example

by Khaleel O.
January 14, 2024
0
0

Let's do a Python Blowfish Encryption example. Let's go! 🔥 🔥 Blowfish is a symmetric-key block cipher algorithm designed for...

Read moreDetails
Python

Python Ternary List Comprehension

by Khaleel O.
January 13, 2024
0
0

python ternary in list comprehension Let's investigate Python ternary list comprehension with some code examples. 🔥🔥 The ternary operator in...

Read moreDetails

DevRescue © 2021 All Rights Reserved. Privacy Policy. Cookie Policy

Manage your privacy

To provide the best experiences, we and our partners use technologies like cookies to store and/or access device information. Consenting to these technologies will allow us and our partners to process personal data such as browsing behavior or unique IDs on this site and show (non-) personalized ads. Not consenting or withdrawing consent, may adversely affect certain features and functions.

Click below to consent to the above or make granular choices. Your choices will be applied to this site only. You can change your settings at any time, including withdrawing your consent, by using the toggles on the Cookie Policy, or by clicking on the manage consent button at the bottom of the screen.

Functional Always active
The technical storage or access is strictly necessary for the legitimate purpose of enabling the use of a specific service explicitly requested by the subscriber or user, or for the sole purpose of carrying out the transmission of a communication over an electronic communications network.
Preferences
The technical storage or access is necessary for the legitimate purpose of storing preferences that are not requested by the subscriber or user.
Statistics
The technical storage or access that is used exclusively for statistical purposes. The technical storage or access that is used exclusively for anonymous statistical purposes. Without a subpoena, voluntary compliance on the part of your Internet Service Provider, or additional records from a third party, information stored or retrieved for this purpose alone cannot usually be used to identify you.
Marketing
The technical storage or access is required to create user profiles to send advertising, or to track the user on a website or across several websites for similar marketing purposes.
Statistics

Marketing

Features
Always active

Always active
Manage options Manage services Manage {vendor_count} vendors Read more about these purposes
Manage options
{title} {title} {title}
Manage your privacy
To provide the best experiences, DevRescue.com will use technologies like cookies to store and/or access device information. Consenting to these technologies will allow us to process data such as browsing behavior or unique IDs on this site. Not consenting or withdrawing consent, may adversely affect certain features and functions.
Functional Always active
The technical storage or access is strictly necessary for the legitimate purpose of enabling the use of a specific service explicitly requested by the subscriber or user, or for the sole purpose of carrying out the transmission of a communication over an electronic communications network.
Preferences
The technical storage or access is necessary for the legitimate purpose of storing preferences that are not requested by the subscriber or user.
Statistics
The technical storage or access that is used exclusively for statistical purposes. The technical storage or access that is used exclusively for anonymous statistical purposes. Without a subpoena, voluntary compliance on the part of your Internet Service Provider, or additional records from a third party, information stored or retrieved for this purpose alone cannot usually be used to identify you.
Marketing
The technical storage or access is required to create user profiles to send advertising, or to track the user on a website or across several websites for similar marketing purposes.
Statistics

Marketing

Features
Always active

Always active
Manage options Manage services Manage {vendor_count} vendors Read more about these purposes
Manage options
{title} {title} {title}
No Result
View All Result
  • Home
  • Python
  • Lists
  • Movies
  • Finance
  • Opinion
  • About
  • Contact Us

DevRescue © 2022 All Rights Reserved Privacy Policy