[BangPypers] Python dictionaries, deleting elements while iterating.

ragsagar ragsagar at gmail.com
Mon Feb 25 17:20:25 CET 2013


On Mon, Feb 25, 2013 at 9:33 PM, Anand Chitipothu <anandology at gmail.com>wrote:

>
> You can iterate over items:
>
> for key, value in dictionary.items():
>     if value == 1:
>         del dictionary[key]
>

I think in Python 3.*, dict.items() returns iterator. If so it won't work
in Python 3.* . So converting it to list would be a better option.

for key in list(dictionary):
   if dictionary[key] == 1:
       del dictionary[key]



-- 
blog : ragsagar.wordpress.com
mail id : python -c "print '@'.join(['ragsagar','.'.join([x for x in
['gmail','com']])])"


More information about the BangPypers mailing list