Moving Places, Subtracting from slices/lists

Fredrik Lundh fredrik at pythonware.com
Thu Jun 2 05:50:52 EDT 2005


Elliot Temple wrote:

> btw hotcat[:] is a *copy* of hotcat, so just leave out "[:]"

when you want to modify the thing you're looping over, you need
to be careful.  looping over a copy is often a good idea (see the
Python tutorial and the FAQ for more on this).

> enumerate is a function that adds indexes to a list.  observe:

>>> hotcat = ['roof', 'roof', 'roof']
>>> for index, word in enumerate(hotcat):
>>>    if word == 'roof': del hotcat[index]
>>> print hotcat
['roof']

if I understand the OP correctly, he wants to *move* the "roof" to the
end of the string.

    try:
        hotcat.remove("root")
        hotcat.append("root")
    except ValueError:
        pass

is most likely the fastest way to do that.

</F> 






More information about the Python-list mailing list