Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove all elements from the dictionary whose key is an element of a list

How do you remove all elements from the dictionary whose key is a element of a list?

like image 243
darkvader Avatar asked Nov 08 '10 01:11

darkvader


People also ask

Which function removes all the elements of a list dictionary?

The clear() method removes all items from a dictionary and makes it empty.

Which dictionary function is used to remove all elements of dictionary dict?

clear() Syntax Here, clear() removes all the items present in the dictionary.

How do you delete all items from a 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.


2 Answers

[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:

  1. How to get a element from a list
  2. How to delete a key:value in dictionary

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.

  • http://docs.python.org/release/2.5.2/lib/typesmapping.html

You should be able to combine the two tasks.

like image 163
pyfunc Avatar answered Nov 15 '22 23:11

pyfunc


for key in list_:
    if key in dict_:
        del dict_[key]
like image 32
rossipedia Avatar answered Nov 16 '22 00:11

rossipedia