473,403 Members | 2,359 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,403 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 2779
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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.