Skip to content
Advertisement

How can I Iterate through lists, without calling each list individually – in Python

I have the following arrays:

JavaScript

And I need to run each array through the following if-else statement:

JavaScript

How can I do this without writing an if-else statement for each array? It’s manageable for now, but I plan to have 25 of these arrays, and I feel like there’s a better way.

Advertisement

Answer

Put all of your votes into a list instead of in 25 different named variables.

JavaScript

You can do the same thing with votes. For example:

JavaScript

Now votes[1] and votes[2] have the same contents as votes_1 and votes_2 in the earlier version of the code, but you can expand this to cover any number of different voting options just by changing that for v in (1, 2) line.

User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement