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. > Well, modifying *any* structure as you iterate over it runs the same risks as sawing through the tree branch you are sitting on.
Since it isn't practical to iterate over Eagle_Power_Nets and test for presence in your Nets dict, you have to iterate over at least the keys of Nets. You could just iterate over the keys rather than the whole table, though: for net in Nets.keys(): # Nets.iterkeys() would avoid building the list # but that runs the same risks as your original if net.upper() in in Eagle_Power_Nets : del Nets[net] Watch that spacing, by the way: Python is easy to keep readable if you follow the style guidelines of PEP 8. regards Steve -- Steve Holden +1 571 484 6266 +1 800 494 3119 Holden Web LLC/Ltd http://www.holdenweb.com Skype: holdenweb http://del.icio.us/steve.holden --------------- Asciimercial ------------------ Get on the web: Blog, lens and tag the Internet Many services currently offer free registration ----------- Thank You for Reading ------------- -- http://mail.python.org/mailman/listinfo/python-list