471,334 Members | 1,755 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,334 software developers and data experts.

removing items from a dictionary ?

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
Jul 26 '07 #1
3 2706
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
Jul 26 '07 #2
On Thu, 26 Jul 2007 21:38:31 +0200, martyw wrote:
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.


--
Steven.

Jul 28 '07 #3
On Jul 28, 1:43 am, Steven D'Aprano
<st...@REMOVE.THIS.cybersource.com.auwrote:
On Thu, 26 Jul 2007 21:38:31 +0200, martyw wrote:
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.

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

- Paddy.

Jul 28 '07 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

12 posts views Thread by Donnal Walter | last post: by
6 posts views Thread by JohnK | last post: by
3 posts views Thread by Jeremy Owens-Boggs | last post: by
2 posts views Thread by =?Utf-8?B?TWFobW91ZCBTaGFiYW4=?= | last post: by
2 posts views Thread by bladedpenguin | last post: by
3 posts views Thread by =?Utf-8?B?YW1pcg==?= | last post: by
reply views Thread by rosydwin | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.