position in a for loop?

wolf castor at snafu.de
Wed Dec 31 10:16:43 EST 2003


you may want to have a look @
http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/204297,
where it is said that 'the interpreter creates a secret 
name that only exists while the list is being built. 
That name is (usually) "_[1]", and it refers to the bound 
method "append" of the list.'. for your specific problem,
the enumerate() solution is probably all that you need;
for other cases (such as checking whether a certain value 
is already a member of the list), this solution may come
in handy. for example, in order to get a non-repetitive 
string with all the characters found in a given text, you 
could say

print ''.join( 
    [ x 
        for x in 'my words are repetitive' 
            if x not in locals()['_[1]'].__self__ ] )

hope that helps, 


_wolf




More information about the Python-list mailing list