[Tutor] Beginner's question: Looping through variable & list simultaneously

Mark Lawrence breamoreboy at yahoo.co.uk
Tue Dec 3 16:39:32 CET 2013


On 03/12/2013 15:03, Rafael Knuth wrote:
> Hej there,
>
>> That's very poor coding, if you're given a function that does exactly what
>> you want, why rewrite it and worse still, get it wrong?
>
> I don't quite understand. I took that advice, tried it - it worked,
> and then I figured out there's also another way to get there.
> The output from the "for Country in range(len(PopularCountries))" is
> exactly the same as with "enumerate", or am I missing something here?
>

We've already established that you've an "off by one" error in the year, 
but let's do a closer analysis of your code and mine.

>>> PopularCountries = ["Brazil", "China", "France", "India", "Vietnam"]
>>> Year = 2009
>>> Backpackers = 1000000
>>> for Country in range(len(PopularCountries)):

"Country" here is actually an index into the list of countries.

>>>       Year += 1

Here's your "off by one" error, it should come after the print function.

>>>       Backpackers = Backpackers*1.15
>>>       print("In %d there were %d backpackers worldwide and their most
>>> popular country was %s." % (Year, Backpackers,
>>> PopularCountries[Country]))

To fetch the country and print it you use your poorly named Country 
index to go back into the list of countries.

>
>> PopularCountries = ["Brazil", "China", "France", "India", "Vietnam"]
>> Backpackers = 1000000
>> for x, PopularCountry in enumerate(PopularCountries, start=2009):
>>      Backpackers = Backpackers*1.15
>>      print("In %d there were %d backpackers worldwide and their most popular
>> country was %s." % (x, Backpackers, PopularCountry))

Here we get the properly initialised index and country in one hit and 
print them directly.  Do you see the difference?  Given that code is 
read far more times than it's written I'd much prefer my version, 
although I'm obviously biased :)  It might not make much odds in a small 
example like this, but in a major project running into possibly millions 
of lines of code you're talking a lot of time and hence money.

>
> Thanks. Just one last question: Is there a way to loop through an
> arbitrary number of lists at the same time?
> Say, if I wanted to loop through the most popular travel guides in
> each year in addition to most popular country? I couldn't figure that
> out by myself.
> Would that be doable with "enumerate" as well?
>

Yes, I've used enumerate with multiple lists, by using the zip function 
Dave Angel mentioned earlier in this thread.  An alternative is to 
perhaps use a list of lists.  Almost inevitably, there is a data 
structure within the standard library or somewhere online (e.g. pypi) 
that'll do the job you want, including looping, without having to 
reinvent wheels.  And if you have to reinvent wheels, it's usually 
better to make them round :)

-- 
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 Tutor mailing list