removing items from a dictionary ?

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


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



More information about the Python-list mailing list