[Tutor] Looking for duplicates within a list

Dave Kuhlman dkuhlman at rexx.com
Sat Jun 12 00:10:43 CEST 2010


On Fri, Jun 11, 2010 at 09:57:34AM -0400, Ken G. wrote:
> 
> 
>    for j in range (0, 5):
>        x = a[0] # for example, 1

One picky, little point.

I've seen several solutions in this thread that included something
like the following:

    for i in range(len(mylist)):
        val = mylist[i]
        mylist[i] = f(val)
        o
        o
        o

That works fine, but ...

You can do this a bit more easily by using the "enumerate" built-in
function.  It provides that index.  Example:

    for i, val in enumerate(mylist):
        mylist[i] = f(val)
        o
        o
        o

See http://docs.python.org/library/functions.html#enumerate

- Dave

-- 
Dave Kuhlman
http://www.rexx.com/~dkuhlman


More information about the Tutor mailing list