Simple exercise

Mark Lawrence breamoreboy at yahoo.co.uk
Thu Mar 10 21:05:33 EST 2016


On 11/03/2016 01:56, Martin A. Brown wrote:
>
>>>> for i in range(len(names)):
>>>>      print (names[i],totals[i])
>>>
>>> Always a code smell when range() and len() are combined.
>>
>> Any other way of traversing two lists in parallel?
>
> Yes.  Builtin function called 'zip'.
>
>    https://docs.python.org/3/library/functions.html#zip
>
> Toy example:
>
>    import string
>    alpha = string.ascii_lowercase
>    nums = range(len(alpha))
>    for N, A in zip(nums, alpha):
>        print(N, A)
>
> Good luck,
>
> -Martin
>

Which would usually be written for N, A in enumerate(alpha):

-- 
My fellow Pythonistas, ask not what our language can do for you, ask
what you can do for our language.

Mark Lawrence




More information about the Python-list mailing list