By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
432,508 Members | 1,856 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 432,508 IT Pros & Developers. It's quick & easy.

Delete elements of dictionary based on value

P: 1
I am looking for an efficient way to delete the elements of a Python dictionary based on an evaluation of the element's value.
Delete all elements with value bigger than x.

Thanks
Jul 10 '06 #1
Share this Question
Share on Google+
1 Reply


P: 12
dvs
Expand|Select|Wrap|Line Numbers
  1. d = {
  2.      'a':10,
  3.      'b':3,
  4.      'c':7,
  5.      'd':8
  6.     }
  7.  
  8. x = 8
  9.  
  10. print dict(
  11.     [(k,v) for k,v in d.items() if v<=x ]
  12.     )
Jul 20 '06 #2

Post your reply

Sign in to post your reply or Sign up for a free account.