Iterate using tuple as index

J

James Stroud

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
 
F

F. Petitjean

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)
 
T

Tim Jarman

James said:
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?
 
B

Bengt Richter

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.,
... print list1_item, list2_item
...
1 a
2 b
3 c


Regards,
Bengt Richter
 
R

Roy Smith

James Stroud said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,222
Messages
2,571,140
Members
47,755
Latest member
Grazynkaa

Latest Threads

Top