Enumerating ordered expat attributes with tuplets?

MRAB google at mrabarnett.plus.com
Thu Sep 11 13:46:27 EDT 2008


On Sep 11, 4:48 pm, Manuel Ebert <maeb... at uos.de> wrote:
> -----BEGIN PGP SIGNED MESSAGE-----
> Hash: SHA1
>
> Ah, well. Don't know whether it meets your aesthetic standards, but:
>  >>> my_list = ['tree', 'hug', 'flower', 'hug', 'bear', 'run']
>  >>> my_list[0:len(a):2]
> ['tree', 'flower', 'bear']
>  >>> my_list[1:len(a):2]
> ['hug', 'hug', 'run']
>
> and hence
>
>  >>> zip(my_list[0:len(a):2], my_list[1:len(a):2])
> [('tree', 'hug'), ('flower', 'hug'), ('bear', 'run')]
>
> and furthermore
>
>  >>> for a, b in zip(my_list[0:len(a):2], my_list[1:len(a):2]):
> ...     print a, b
> ...
> tree hug
> flower hug
> bear run
>
> or the slightly less obfuscated:
>
>  >>> for index in range(0, len(my_list), 2):
> ...     print my_list[index], my_list[index + 1]
> ...    
> tree hug
> flower hug
> bear run
>
[snip]
I don't know what the "len(a)" is, but the end position defaults to
the end:

>>> zip(my_list[0::2], my_list[1::2])
[('tree', 'hug'), ('flower', 'hug'), ('bear', 'run')]



More information about the Python-list mailing list