[Tutor] Iterating over a string: index and value

Carroll, Barry Barry.Carroll at psc.com
Fri Feb 10 22:55:41 CET 2006


Adam,

That is super!  Just what I was looking for.  Thanks!

And whaddya know?  There it is in the Python 2.3 Library reference, section 2.1!

Regards,
 
Barry
barry.carroll at psc.com
541-302-1107
________________________
"Never trust anything that can think for itself 
if you can't see where it keeps its brain" 
JK Rowling
 
>________________________________________
>From: Adam [mailto:adam.jtm30 at gmail.com] 
>Sent: Friday, February 10, 2006 1:42 PM
>To: Carroll, Barry
>Cc: tutor at python.org
>Subject: Re: [Tutor] Iterating over a string: index and value
>
>Here's a list comprehension which does it:
>>>> print [(i, ord(v)) for i, v in enumerate("abcdefg")]
>[(0, 97), (1, 98), (2, 99), (3, 100), (4, 101), (5, 102), (6, 103)]
>
>and a for loop:
>
>>>> for i, v in enumerate("abcdefg"):
>...     tuplseq.append((i, ord(v)))
>...
>>>> tuplseq
>[(0, 97), (1, 98), (2, 99), (3, 100), (4, 101), (5, 102), (6, 103)]
>
>how's that?



More information about the Tutor mailing list