How do you remove all elements from the dictionary whose key is a element of a list?
The clear() method removes all items from a dictionary and makes it empty.
clear() Syntax Here, clear() removes all the items present in the dictionary.
Clear() methods are used to remove elements of the collection. The Remove method removes elements matching with the key passed in the Remove method. The Clear method removes all elements from the dictionary.
[Note: This is not direct answer but given earlier speculation that the question looks like homework. I wanted to provide help that will help solving the problem while learning from it]
Decompose your problem which is:
Further help:
How do get all element of a list on python?
For loop works on all sequences and list is a sequence.
for key in sequence: print key
How do you delete a element in dictionary?
use the del(key) method.
You should be able to combine the two tasks.
for key in list_:
if key in dict_:
del dict_[key]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With