[Tutor] solution for for loop?

Clayton Kirkwood crk at godblessthe.us
Sun Oct 26 00:40:54 CEST 2014


Ding, ding, ding. Winner, winner, winner. Peter wins the prize, suggesting
the use of zip(). This allowed me to pull two different strings from two
different lists at one time in a for loop.

for description_position, code_position in zip(description, code):

Yes!!  Thank you Peter

Clayton

!-----Original Message-----
!From: Tutor [mailto:tutor-bounces+crk=godblessthe.us at python.org] On
!Behalf Of Peter Otten
!Sent: Saturday, October 25, 2014 4:41 AM
!To: tutor at python.org
!Subject: Re: [Tutor] solution for for loop?
!
!Clayton Kirkwood wrote:
!
!> description_string=code_string=''
!>
!> description = code = 'a'
!>
!> for (description_position, code_position) in (description, code):
!>
!>     print(description_position,code_position)
!
!> I have tried variations on this for statement, and it doesn't
!> work:<))) Both description and code have the same size array. I was
!> hoping that some derivative of this for would bring in a new
!> description_position value, and code_position value.
!
!You want zip():
!
!>>> colors = [ "red", "green", "yellow"] fruit_list = ["cherry",
!>>> "apple", "banana"] for color, fruit in zip(colors, fruit_list):
!...     print(color, fruit)
!...
!red cherry
!green apple
!yellow banana
!
!
!_______________________________________________
!Tutor maillist  -  Tutor at python.org
!To unsubscribe or change subscription options:
!https://mail.python.org/mailman/listinfo/tutor





More information about the Tutor mailing list