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
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
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
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
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
"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
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
"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
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
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
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
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
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
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
14 matches
Mail list logo