Loop two variables simultaneously in Python 3?


I would like to display two columns, like this:

1 10
2 9
3 8

5 6

How would I do this? Would it look something like this? I don’t want to use the same variable.

for i, j in range(1, 6 + 1), range(10, 5, -1):
print("{0:10d}{1: 10d}".format(I, j))

Close. The for-loop expects an iterable of pairs, so you’d need a function which pairs up those two ranges:

zip(range(1, 7), range(10, 5, -1))

Or if you have some better way to create pairs, because that’s slightly messy/difficult to read. In this particular case the numbers always have a sum of 11, so you could also compute one number from the other… Exactly how you decide to write it comes down to good taste really… There will be many ways to express it, and what those ways are depends on what you’re describing

(you need something that looks like this: [(1, 2), (3, 4)], to be unpacked one pair at a time )

In actual program, I wanted one column to start at 40, decrease by 1 all the way to 31 and the second column starts at 120 and goes down by 10 all the way to 30

I might go about it like this:

for offset in range(10):
    c1 = 40 - offset
    c2 = 120 - offset * 10

…But I might do it differently understanding the whole situation. In the end, you should go with whatever looks best to you, and it can be hard to come up with a “best” without having seen lots of other code to draw inspiration from.

Either you can argue for one being better than another, or it doesn’t matter and you just pick one and move on, hopefully without leaving a mess.

A comment describing intention helps a lot if it can be better described in plain writing, this is precisely the kind of thing comments are for.

Thanks a lot! Also one more thing. When I have many functions. I have a hard time naming my variables. I don’t know if this is right, but I always give my variables descriptive names. For example number_of_balls. But when I have many functions that uses the same information I have to rename (to differentiate it and also I thought it is bad style to use the same local variable names in different functions) it like: num_balls or ball_number, total_number_ball, etc. Is that the right way to name variables or should I just use variable names like I, j, k (or is that only for loops)?

i, j, k should only be used if it’s a dumb variable just used for counting with no real meaning, likely meaning index or iteration

Yes, name variables after what the values they refer to represent. (Which means plural for lists and such)

If a function is self-contained then you don’t need to worry about what names other functions might use, but you might want consistent naming if there are concepts shared throughout the program

Could you give an example of what you mean by “you might want consistent naming if there are concepts shared throughout the program”.

Don’t mix farenheit and celcius unless a function specifically has to do with either unit

Great. Thanks! Very helpful!

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.