for loop question
python.list at tim.thechases.com
Wed Oct 10 22:12:48 CEST 2007
> test = u"Hello World"
> for cur,next in test:
> print cur,next
> Ideally, this would output:
> 'H', 'e'
> 'e', 'l'
> 'l', 'l'
> 'l', 'o'
> Of course, the for loop above isn't valid at all. I am just giving an
> example of what I'm trying to accomplish. Anyone know how I can achieve the
> goal in the example above? Thanks.
>>> pairs = (test[i:i+2] for i in xrange(len(test)-1))
>>> for a,b in pairs:
... print a,b
More information about the Python-list