473,239 Members | 1,800 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,239 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 2774
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

12
by: Donnal Walter | last post by:
The following method is defined in one of my classes: def setup(self, items={}): """perform setup based on a dictionary of items""" if 'something' in items: value = items # now do something...
6
by: JohnK | last post by:
ok, ya got me here. I'm trying to removing items from a dictionary inside a loop. Obviously using enumeration does not work as that assumes the dictionary stays unchanged. So how so I iterate...
3
by: Jeremy Owens-Boggs | last post by:
We are trying to implement a dual list box selection where you have two list boxes, You highlight items in the right side list box, click a button and this moves those items over to the left hand...
6
by: Niyazi | last post by:
Hi all, What is fastest way removing duplicated value from string array using vb.net? Here is what currently I am doing but the the array contains over 16000 items. And it just do it in 10 or...
17
by: Eric_Dexter | last post by:
def simplecsdtoorc(filename): file = open(filename,"r") alllines = file.read_until("</CsInstruments>") pattern1 = re.compile("</") orcfilename = filename + "orc" for line in alllines: if not...
5
by: Jen | last post by:
The usual trick to get around the "collection was modified" problem by looping through the collection backwards by index won't work with a Hashtable because there is no way to access by index,...
2
by: =?Utf-8?B?TWFobW91ZCBTaGFiYW4=?= | last post by:
i have a problem in adding new listbox items i don't need to allow adding multible items with the same textvalue ex: if current items are: green red
2
by: bladedpenguin | last post by:
I am writing a game, and it must keep a list of objects. I've been representing this as a list, but I need an object to be able to remove itself. It doesn't know it's own index. If I tried to make...
3
by: =?Utf-8?B?YW1pcg==?= | last post by:
Hi, I have a Generic Object that has a private List field item. I populate the List in a different function I use a FOREACH LOOP with a FindAll function to get all items that have a certain...
0
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...

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.