[Tutor] for loops over multiple lists of the same length

Emily Fortuna emily.fortuna at nist.gov
Thu Jun 22 19:22:54 CEST 2006


I feel like there should be a better way to do this process:
Can you please help?
(This is trivial example code I created off the top of my head, but the 
same concept that I am trying to do elsewhere.)

class Person(object):
	def __init__(self, first_name, age, fav_color):
		self.first_name = first_name
		self.age = age
		self.fav_color = fav_color

first_names = ['emily', 'john', 'jeremy', 'juanita']
ages = [6, 34, 1, 19]
colors = ['blue', 'orange', 'green', 'yellow']

ageIter = ages.iter()
colorIter = colors.iter()
people = [Person(name, ageIter.next(), colorIter.next()) for name in 
first_names]
	
print people

any suggestions, please?
Emily



More information about the Tutor mailing list