[Tutor] Iterate over multiple objects

W W srilyk at gmail.com
Wed Oct 29 12:30:22 CET 2008


Hi,
I'm trying to compare two strings because I want to find the difference.

i.e.
string1 = "foobar"
string2 = "foobzr"

is there a simple way to do this with a for loop? This is the method I
tried, but it gives me an error:

In [14]: for x, y in bar[0], bar[1]:
   ....:     print x, y
   ....:
   ....:
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)

D:\Documents and Settings\Wayne\<ipython console> in <module>()

ValueError: too many values to unpack

I suppose I could do:

for x in xrange(0, len(bar[0])):
    print bar[0][x], bar[1][x]    #yes I realize there's no comparison here,
I know how to do that - this is just a placeholder

Would that be the "best"/most pythonic solution?

Thanks,
Wayne

-- 
To be considered stupid and to be told so is more painful than being called
gluttonous, mendacious, violent, lascivious, lazy, cowardly: every weakness,
every vice, has found its defenders, its rhetoric, its ennoblement and
exaltation, but stupidity hasn't. - Primo Levi
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20081029/d0a333e9/attachment.htm>


More information about the Tutor mailing list