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


Messages In This Thread
Using modulo to loop through lists - by fistfullofnails - 08-25-2025, 02:15 PM

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,188 12-19-2025, 12:51 PM
Last Post: Heimdall
  Exiting sub while inside a loop PhilOfPerth 5 507 12-05-2025, 09:40 AM
Last Post: PhilOfPerth
  Do Loop, Sleep and Mouse Button Dimster 5 583 09-06-2025, 12:57 PM
Last Post: Dimster
  What is wrong with this for/next loop Helium5793 6 1,121 04-15-2025, 05:11 PM
Last Post: Kernelpanic
  Question on ln in a for/next loop Dimster 13 2,183 09-13-2024, 11:07 PM
Last Post: Kernelpanic

Forum Jump:


Users browsing this thread: 1 Guest(s)