469,328 Members | 1,266 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,328 developers. It's quick & easy.

Iterate using tuple as index

Hello,

Its not obvious to me how to do this. I would like to iterate using a tuple as
an index. Say I have two equivalently sized arrays, what I do now seems
inelegant:

for index, list1_item in enumerate(firstlist):
do_something(list1_item, secondlist[index])

I would like something more like this:

for list1_item, list2_item in (some_kind_of_expression):
do_something(list1_item, list2_item)

Practically, I'm not so sure B is better than A, but the second would be a
little more aesthetic, to me, at least.

Any thoughts on what "some_kind_of_expression" would be?

James

--
James Stroud, Ph.D.
UCLA-DOE Institute for Genomics and Proteomics
Box 951570
Los Angeles, CA 90095
Jul 18 '05 #1
4 1523
Le Thu, 10 Mar 2005 13:12:31 -0800, James Stroud a écrit :
Hello,

Its not obvious to me how to do this. I would like to iterate using a tuple as
an index. Say I have two equivalently sized arrays, what I do now seems
inelegant:

for index, list1_item in enumerate(firstlist):
do_something(list1_item, secondlist[index])

I would like something more like this:

for list1_item, list2_item in (some_kind_of_expression):
do_something(list1_item, list2_item)

Practically, I'm not so sure B is better than A, but the second would be a
little more aesthetic, to me, at least.

Any thoughts on what "some_kind_of_expression" would be? It is called "zip"
for list1_item, list2_item in zip(firstlist, secondlist):
....
try: help(zip)
James

Jul 18 '05 #2
James Stroud wrote:
Hello,

Its not obvious to me how to do this. I would like to iterate using a
tuple as an index. Say I have two equivalently sized arrays, what I do now
seems inelegant:

for index, list1_item in enumerate(firstlist):
do_something(list1_item, secondlist[index])

I would like something more like this:

for list1_item, list2_item in (some_kind_of_expression):
do_something(list1_item, list2_item)

Practically, I'm not so sure B is better than A, but the second would be a
little more aesthetic, to me, at least.

Any thoughts on what "some_kind_of_expression" would be?

James


for item1, item2 in zip(list1, list2):
do_something(item1, item2)

perhaps?

--
Website: www DOT jarmania FULLSTOP com
Jul 18 '05 #3
On Thu, 10 Mar 2005 13:12:31 -0800, James Stroud <js*****@mbi.ucla.edu> wrote:
Hello,

Its not obvious to me how to do this. I would like to iterate using a tuple as
an index. Say I have two equivalently sized arrays, what I do now seems
inelegant:

for index, list1_item in enumerate(firstlist):
do_something(list1_item, secondlist[index])

I would like something more like this:

for list1_item, list2_item in (some_kind_of_expression):
do_something(list1_item, list2_item)

Practically, I'm not so sure B is better than A, but the second would be a
little more aesthetic, to me, at least.

Any thoughts on what "some_kind_of_expression" would be?

zip?
firstlist = [1,2,3]
secondlist = 'a b c'.split()
firstlist, secondlist ([1, 2, 3], ['a', 'b', 'c']) for list1_item, list2_item in zip(firstlist, secondlist): ... print list1_item, list2_item
...
1 a
2 b
3 c

Or if your lists are very long, you could use an iterator from itertools, e.g.,
import itertools
for list1_item, list2_item in itertools.izip(firstlist, secondlist):

... print list1_item, list2_item
...
1 a
2 b
3 c
Regards,
Bengt Richter
Jul 18 '05 #4
James Stroud <js*****@mbi.ucla.edu> wrote:
I would like something more like this:

for list1_item, list2_item in (some_kind_of_expression):
do_something(list1_item, list2_item)


I believe you want:

for list1_item, list2_item in zip (list1, list2):
blah
Jul 18 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

50 posts views Thread by Will McGugan | last post: by
3 posts views Thread by BJörn Lindqvist | last post: by
15 posts views Thread by Steve M | last post: by
5 posts views Thread by Bas Scheffers | last post: by
6 posts views Thread by groups.20.thebriguy | last post: by
6 posts views Thread by alainpoint | last post: by
13 posts views Thread by stef mientki | last post: by
6 posts views Thread by nielsp | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
reply views Thread by listenups61195 | last post: by
reply views Thread by Purva khokhar | last post: by
reply views Thread by haryvincent176 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.