[Tutor] Re: For statement
Andrei
project5 at redrival.net
Fri Mar 12 14:11:18 EST 2004
Øyvind Dale Spørck wrote on Thu, 11 Mar 2004 18:31:59 +0100:
> I have a list:
>
> a= ['golden,','poodle,','flat,','retriever,']
>
> I would like to remove the , after the word, with a for statement. However, the result doesn't end up as I like. I end up removing one of the items at the time or getting a "TypeError: list indices must be integers" error.
>
> I have tried tons of different versions such as:
>
>>>> for x in a:
In this case x will be an item inside the list, so in the first run of the
loop x='golden,', in the second run x='poodle,', etc. This is nice if you
just need the items, but it's not nice if you want to manipulate the list
because you have no idea where in the list you are.
So in this case you need to loop by index. Items in a list are indexed
starting with 0 and ending with len(MyList)-1. So in your list we have:
len(a) == 4
a[0] == 'golden,'
a[3] == 'retriever,'
The range() function happens to work terribly well with lists, like this:
for i in range(len(a)):
print i, a[i]
Try that in the Python interpreter. It's now obvious that using this loop,
you could replace the print statement with the code which manipulates a[i]:
the item at position i in list a. This is the stuff you're looking for, but
I'll comment on the code you posted as well.
> ... a[:] = a[:][:-1]
a[:] simply returns a copy of a. So we can pretty much reduce this
statement to:
a = a[:-1]
Which means all of a except the last item (last item has index -1 as well
as index len(a)-1 ). Since you're looping over the list, you'll try to
strip off one item of the list in every loop, so you'll end up with two
items instead of 4.
This also demonstrates that you should *never* manipulate a list used as
loop control variable inside that sime loop. You were probably expecting to
have four loops, but you only get two. What happens is this:
At the start of the first loop:
a = [0,1,2,3]
^ (^ indicates what the loop is reading from a; looping
will continue until ^ reaches the end of the list)
First loop: last item is stripped off:
a = [0,1,2]
^
Loop is ended, position moves one forward:
a = [0,1,2]
^
Second loop is started and executed, last item is stripped again:
a = [0,1]
^
Second loop is done. But now a is only two items large which means that the
^ has reached the end after only two loops instead of 4 as we expected.
Another tip: if you got those items from a single string like:
a = 'golden, poodle, flat, retriever'.split()
you could have used:
a = 'golden, poodle, flat, retriever'.split(', ')
to get the same results in one go.
Last but not least: list comprehensions are terribly useful for this kind
of list manipulations. You should have a look at them, they're quite nifty
and very easy to use once you understand how they work.
--
Yours,
Andrei
=====
Real contact info (decode with rot13):
cebwrpg5 at jnanqbb.ay. Fcnz-serr! Cyrnfr qb abg hfr va choyvp cbfgf. V ernq
gur yvfg, fb gurer'f ab arrq gb PP.
More information about the Tutor
mailing list