removing items from a dictionary ?

S

Stef Mientki

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
 
M

martyw

Stef said:
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
 
B

Ben Finney

Stef Mientki said:
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 ]

Don't change the thing you're currently iterating over. Instead,
iterate over a copy:
>>> eagle_power_nets = ['eggs', 'beans']
>>> nets = {'spam': 10, 'eggs': 20, 'ham': 30}
>>> for name in list(nets.keys()):
... if name in eagle_power_nets:
... del nets[name]
... {'ham': 30, 'spam': 10}


Style hints (from <URL:http://www.python.org/dev/peps/pep-0008/>):

* Don't name an instance with Upper_Case; the convention is for
instances to be named as lower_case_with_underscores, and classes to
be named in TitleCase.

* Don't put spaces inside the bracketing characters '[]', '{}', '()'.

* Don't separate the item specifier '[foo]' or the function parameters
'(bar)' from the identifier; instead follow it immediately,
'foo_list[foo]', 'bar_func(bar)'.

None of these are mandatory, but they'll make your code comply with
what Python programmers expect and thus be more easily maintained.
 
S

Steven D'Aprano

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


Wow. This wins my award for the least helpful, while still being
technically correct, reply ever. Did you even read the Original Poster's
post? He already knows that you delete items from a dictionary with del,
and he posted code and the traceback he gets when he runs it.
 
P

Paddy

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

Wow. This wins my award for the least helpful, while still being
technically correct, reply ever. Did you even read the Original Poster's
post? He already knows that you delete items from a dictionary with del,
and he posted code and the traceback he gets when he runs it.

.... But lets also applaud the fact that MartyW wants to help.

- Paddy.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,982
Messages
2,570,185
Members
46,736
Latest member
AdolphBig6

Latest Threads

Top