Re: [Tutor] Remove a dictionary entry

2010-09-19 Thread Peter Otten
Steven D'Aprano wrote: > On Sat, 18 Sep 2010 07:13:13 pm Peter Otten wrote: > >> You should never iterate over a list or dictionary and add or remove >> items to it at the same time. That is a recipe for disaster even if >> it doesn't fail explicitly. > > That's a bit strong. It's quite possible

Re: [Tutor] Remove a dictionary entry

2010-09-19 Thread Peter Otten
M. 427 wrote: > Version 4 : (2 steps) > > # step 1 : list keys of unwanted rows > sck=[] # list of single children keys in dictionary > for k in d.keys() : > if len(d[k]) < 2 : > sck.append(k) > # step 2 : delete all d rows w

Re: [Tutor] Remove a dictionary entry

2010-09-19 Thread M. 427
Version 4 : (2 steps) # step 1 : list keys of unwanted rows sck=[] # list of single children keys in dictionary for k in d.keys() : if len(d[k]) < 2 : sck.append(k) # step 2 : delete all d rows whose key is listed in sck

Re: [Tutor] Remove a dictionary entry

2010-09-18 Thread bob gailer
Yet another way is to iterate thru the dict collecting a list of keys of items to be deleted. Then iterate thru that list deleting from the dict. -- Bob Gailer 919-636-4239 Chapel Hill NC ___ Tutor maillist - Tutor@python.org To unsubscribe or cha

Re: [Tutor] Remove a dictionary entry

2010-09-18 Thread Steven D'Aprano
On Sat, 18 Sep 2010 07:13:13 pm Peter Otten wrote: > You should never iterate over a list or dictionary and add or remove > items to it at the same time. That is a recipe for disaster even if > it doesn't fail explicitly. That's a bit strong. It's quite possible to modify lists safely and correc

Re: [Tutor] Remove a dictionary entry

2010-09-18 Thread Alan Gauld
"Alan Gauld" wrote I ended up with this : Version 3 : for i,row in d[:].iteritems() : # BUG : TypeError: unhashable type if len(row) < 2 : del d[i] You are getting too complicated. You don't need the slice and you don't need iteritems. You have a dictionary. When you iterate over

Re: [Tutor] Remove a dictionary entry

2010-09-18 Thread Peter Otten
M. 427 wrote: > (I am very new to python) > I built a dictionary d={} of lists similar to this : > > d = { > 'a': ['apricot', 'apple'], > 'b': ['beach', 'bear', 'bottle'], > 'c': ['cold', 'cook', 'coleslaw'], > 'd': ['deep'], > 'e': ['expression', 'elephant'] > } > > Now i want to go through thi

Re: [Tutor] Remove a dictionary entry

2010-09-18 Thread Alan Gauld
"M. 427" <4...@free.fr> wrote I ended up with this : Version 3 : for i,row in d[:].iteritems() : # BUG : TypeError: unhashable type if len(row) < 2 : del d[i] You are getting too complicated. You don't need the slice and you don't need iteritems. You have a dictionary. When you ite

Re: [Tutor] Remove a dictionary entry

2010-09-18 Thread M. 427
Thank you, After reading the following documentations http://docs.python.org/tutorial/datastructures.html#looping-techniques http://docs.python.org/tutorial/controlflow.html#for-statements I ended up with this : Version 3 : for i,row in d[:].iteritems() : # BUG : TypeError: unhashable type if

Re: [Tutor] Remove a dictionary entry

2010-09-17 Thread bob gailer
On 9/17/2010 9:21 PM, M. 427 wrote: Thank you, After reading the following documentations http://docs.python.org/tutorial/datastructures.html#looping-techniques http://docs.python.org/tutorial/controlflow.html#for-statements I ended up with this : Version 3 : for i,row in d[:].iteritems() : # B

Re: [Tutor] Remove a dictionary entry

2010-09-17 Thread bob gailer
Please always reply-all so a copy goes to the tutor list. On 9/17/2010 6:20 PM, M. 427 wrote: Thank you very much for your answer. I wanted to know the pythonic way of doing this, so I did not post my buggy trial which was : version 1 : for row in d : if len(row) == 1 : del row # WRONG

Re: [Tutor] Remove a dictionary entry

2010-09-17 Thread bob gailer
On 9/17/2010 9:08 AM, M. 427 wrote: Hello, (I am very new to python) I built a dictionary d={} of lists similar to this : d = { 'a': ['apricot', 'apple'], 'b': ['beach', 'bear', 'bottle'], 'c': ['cold', 'cook', 'coleslaw'], 'd': ['deep'], 'e': ['expression', 'elephant'] } Now i want to go thr

Re: [Tutor] Remove a dictionary entry

2010-09-17 Thread Joel Goldstick
On Fri, Sep 17, 2010 at 9:08 AM, M. 427 <4...@free.fr> wrote: > Hello, > > (I am very new to python) > I built a dictionary d={} of lists similar to this : > > d = { > 'a': ['apricot', 'apple'], > 'b': ['beach', 'bear', 'bottle'], > 'c': ['cold', 'cook', 'coleslaw'], > 'd': ['deep'], > 'e': ['expr

[Tutor] Remove a dictionary entry

2010-09-17 Thread M. 427
Hello, (I am very new to python) I built a dictionary d={} of lists similar to this : d = { 'a': ['apricot', 'apple'], 'b': ['beach', 'bear', 'bottle'], 'c': ['cold', 'cook', 'coleslaw'], 'd': ['deep'], 'e': ['expression', 'elephant'] } Now i want to go through this dictionary and remove all ro