removing items from a dictionary ?

Stef Mientki S.Mientki-nospam at
Thu Jul 26 21:03:17 CEST 2007


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
     ?	D:\data_to_test\JALsPy\	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.

Stef Mientki

More information about the Python-list mailing list