How to Check the Size of a Deque in Python?

Estimated read time 1 min read

To check the size of a deque in Python, you can use the len() function. The len() function returns the number of elements in a collection, including deques. Here’s an example:

from collections import deque

# Create a deque
my_deque = deque([1, 2, 3, 4, 5])

# Get the size of the deque
deque_size = len(my_deque)

# Print the size of the deque

In the code above, we first import the deque class from the collections module. Then, we create a deque called my_deque containing some elements. Next, we use the len() function to retrieve the size of the deque, and assign it to the variable deque_size. Finally, we print the size of the deque using the print() function.

The output will be 5, indicating that the deque has 5 elements.

You May Also Like

More From Author

+ There are no comments

Add yours

Leave a Reply