By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,627 Members | 1,211 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,627 IT Pros & Developers. It's quick & easy.

Iterate using tuple as index

P: n/a
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
Share this Question
Share on Google+
4 Replies


P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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.