Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on May 16th, 2012  |  syntax: None  |  size: 0.38 KB  |  hits: 17  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. Remove more than one key from Python dict
  2. x = {'a': 5, 'b': 2, 'c': 3}
  3. x.pop('a', 'b')
  4. print x
  5. {'c': 3}
  6.        
  7. x = {'a': 5, 'b': 2, 'c': 3}
  8. del x['a'], x['b']
  9. print x
  10. {'c': 3}
  11.        
  12. >>> x={'a':5,'b':2,'c':3,1:'abc',2:'efg',3:'xyz'}
  13. >>> [k for k in x if type(k) == str]
  14. ['a', 'c', 'b']
  15.        
  16. >>> for key in [k for k in x if type(k) == str]: del x[key]
  17. >>> x
  18. {1: 'abc', 2: 'efg', 3: 'xyz'}