Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Using modulo to loop through lists
#1
I made the mistake of looking at some python the other day and I noticed that they were using the modulo to loop through a list.  As in if there was a list of four items, the loop would contain something like 
Code: (Select All)
items[x%4]
.  So that being said, what's throwing me for a loop, is that I can see how that works once 'x' becomes >= than 4, but i am stumped for when x would equal 0 to 3.  Would that not be 0/4, 1/4, 2/4, and 3/4?  I understand that 0/4 would have a remainder of 0, but then, to me at least, 1/4 through 3/4 would not have a whole number remainder.  So how would 'items[1%4]' give you the second item in the list?  To me, the remainder of 1 divided by 4 is not equal to 1.  It's 0.25.  If I actually type out 1%4 in my prompt, it does return 1, so apparently the computer agrees with it.
Reply
#2
Remember, MOD deals with integer math.

6 MOD 4 would be like saying:   6 - (INT(6 / 4) * 4)... or basically 6 - (1 * 4)... 6 - 4... 2

So if we say 1 MOD 4 it's the same basic math as 1 - (INT(1 / 4) * 4).

What is 1/4?  That 0.25.   So do the substitution:   1 - (INT(0.25) * 4)
What's the INT(0.25)?  That's 0.  Do that substitution.  1 - (0 * 4)
Do the multiplication in the parenthesis:   1 - 0
And then do the subtraction:  1

1 MOD 4 = 1
Reply
#3
(08-25-2025, 02:15 PM)fistfullofnails Wrote: I made the mistake of looking at some python the other day and I noticed that they were using the modulo to loop through a list.  As in if there was a list of four items, the loop would contain something like 
Code: (Select All)
items[x%4]
.  So that being said, what's throwing me for a loop, is that I can see how that works once 'x' becomes >= than 4, but i am stumped for when x would equal 0 to 3.  Would that not be 0/4, 1/4, 2/4, and 3/4?  I understand that 0/4 would have a remainder of 0, but then, to me at least, 1/4 through 3/4 would not have a whole number remainder.  So how would 'items[1%4]' give you the second item in the list?  To me, the remainder of 1 divided by 4 is not equal to 1.  It's 0.25.  If I actually type out 1%4 in my prompt, it does return 1, so apparently the computer agrees with it.

To add on to what Steve said, remember a remainder is always a whole number, it's what you get when you stop doing division after calculating only the whole number part of the answer and then checking what number is "left over".

In the case of `1/4`, the whole number part of the answer is zero, so the "left over" part is 1. You're seeing it instead as .25 because you're continuing to do the division after you've already calculated the whole number part of the answer.
Reply
#4
Thank both of you for your help
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Question Experimenting with a "StringList" type for simpler handling of string arrays/lists Heimdall 18 1,183 12-19-2025, 12:51 PM
Last Post: Heimdall
  Exiting sub while inside a loop PhilOfPerth 5 506 12-05-2025, 09:40 AM
Last Post: PhilOfPerth
  Do Loop, Sleep and Mouse Button Dimster 5 578 09-06-2025, 12:57 PM
Last Post: Dimster
  What is wrong with this for/next loop Helium5793 6 1,120 04-15-2025, 05:11 PM
Last Post: Kernelpanic
  Question on ln in a for/next loop Dimster 13 2,180 09-13-2024, 11:07 PM
Last Post: Kernelpanic

Forum Jump:


Users browsing this thread: 1 Guest(s)