w3resource

Python: Remove all the elements of a given deque object

Python Collections: Exercise-10 with Solution

Write a Python program to remove all the elements of a given deque object.

Sample Solution:

Python Code:

import collections
odd_nums = (1,3,5,7,9)
odd_deque  = collections.deque(odd_nums)
print("Original Deque object with odd numbers:")
print(odd_deque)
print("Deque length: %d"%(len(odd_deque)))
odd_deque.clear()
print("Deque object after removing all numbers-")
print(odd_deque)
print("Deque length:%d"%(len(odd_deque)))

Sample Output:

Original Deque object with odd numbers:
deque([1, 3, 5, 7, 9])
Deque length: 5
Deque object after removing all numbers-
deque([])
Deque length:0

Flowchart:

Python Collections: Remove all the elements of a given deque object.

Visualize Python code execution:

The following tool visualize what the computer is doing step-by-step as it executes the said program:

Python Code Editor:

Have another way to solve this solution? Contribute your code (and comments) through Disqus.

Previous: Write a Python program to add more number of elements to a deque object from an iterable object.
Next: Write a Python program to copy of a deque object and verify the shallow copying process.

What is the difficulty level of this exercise?

Test your Python skills with w3resource's quiz


Python: Tips of the Day

Creates a dictionary with the same keys as the provided dictionary and values generated by running the provided function for each value:

Example:

def tips_map_values(obj, fn):
  ret = {}
  for key in obj.keys():
    ret[key] = fn(obj[key])
  return ret
users = {
  'Owen': { 'user': 'Owen', 'age': 29 },
  'Eddie': { 'user': 'Eddie', 'age': 15 }
}

print(tips_map_values(users, lambda u : u['age'])) # {'Owen': 29, 'Eddie': 15}

Output:

{'Owen': 29, 'Eddie': 15}