removing items from a dictionary ?

martyw nomail at noserve
Thu Jul 26 15:38:31 EDT 2007


Stef Mientki wrote:
> hello,
> 
> I want to remove some items from a dictionary,
> so I would expect this should work:
> 
>   Nets = {}
>   ... fill the dictionary Nets
> 
>   for net in Nets:
>     if net.upper() in Eagle_Power_Nets :
>       del Nets [ net ]
> 
> 
> But it gives me
> Message    File Name    Line    Position
> Traceback           
>     ?    D:\data_to_test\JALsPy\Eagle_import.py    380   
> RuntimeError: dictionary changed size during iteration           
> 
> 
> Now I can solve this problem in the following way
> 
>   power_nets = []
>   for net in Nets:
>     if net.upper() in Eagle_Power_Nets :
>       power_nets.append ( net )
> 
>   # remove power nets from netlist
>   for net in power_nets:
>      del Nets [ net ]
> 
> 
> But I wonder if this is the best way to manipulate a dictionary,
> because I've to do more "complex" operations on the dictionary,
> like joining items,
> I would like to have a better understanding of what can and what can't 
> be done.
> 
> thanks,
> Stef Mientki
Remoing elements from a dict is done with del, try this;
 >>> d = {'a' : 1,'b' : 2}
 >>> del d['a']
 >>> print d
{'b': 2}
 >>>

maybe you can post a working snippet to demonstrate your problem



More information about the Python-list mailing list