[Tutor] need futher explaining

Kent Johnson kent37 at tds.net
Mon Aug 6 13:52:21 CEST 2007


Dale Pearl wrote:
> I'm reading Beginning Python - From Novice to Professional by Magnus Lie 
> Hetland (an Apress book) and there is a code example that I need further 
> explaining on to fully grasp.
> There is a section with samle code of:
>  names = ['anne', 'beth', 'george', 'damon']
> ages = [12, 45, 32, 102]
> for i in range(len(names)):
> print names[i], 'is', ages[i], 'years old'

bhaaluu answered your question, I just want to comment that IMO this is 
not idiomatic Python. A better way to write it would be to combine the 
two lists using zip():

for name, age in zip(names, ages):
   print name, 'is', age, 'years old'

zip: http://docs.python.org/lib/built-in-funcs.html#l2h-81

Kent


More information about the Tutor mailing list