Quirky Python Loop Thing

Please don’t take this as a "Here is how you should do this in Python" post but rather, "I found this interesting behavior and thought I’d share it" instead. I know I’ve said this a million times on my …


This content originally appeared on Raymond Camden and was authored by Raymond Camden

Please don't take this as a "Here is how you should do this in Python" post but rather, "I found this interesting behavior and thought I'd share it" instead. I know I've said this a million times on my blog already, but I'm learning Python and try my best to take every opportunity I can to practice it. Currently I'm using Python for this year's Advent of Code, and while I'm already behind, I've enjoyed it so far.

In my work on day three's solutions, I ran into an interesting issue. As part of the process for solving the second half of the puzzle, I needed to look at an array of data in groups of three. It was safe to assume that my input would be evenly divided by that number. So for example, this array of 9 characters:

input = ["a","b","c","d","e","f","g","h","i"]

Normally I loop over an array like so:

for i in input:	print(i)

Given that prints every item, how can you print every third item? Turns out that's super simple. When working with arrays, you can provide a start, end, and count value in brackets. So for example:

input[start:end:count]

Even cooler, you can leave off start and end:

input[:count]

So for example:

for i in input[::3]:	print(i)

Which returns:

adg

Perfect! Except that in order to work with my "groups", I needed to know the current index I was on. Having the index would then let me simple "plus one" and "plus two" to get a group. Remember, it was safe to assume the array was divisble by three.

So... again, remember I'm still learning this. In order to get the current index, I turned to enumerate and tried this:

for idx, i in enumerate(input[::3]):	print(idx,i)	

And got... not what I expected:

0 a1 d2 g

As you can see, instead of 0, 3, 6, I got 0, 1, 2. Which... I guess represents the current loop iteration and kinda makes sense, but as I said, it wasn't what I expected. How did I get around it?

In my Advent of Code solution, I just switched to a while loop:

while idx + 3 <= len(input):	ourGroup =	[		input[idx], 		input[idx+1],		input[idx+2]	]	# more stuff here... 	idx = idx + 3

But later I realized I could simply multiply by 3:

for idx, i in enumerate(input[::3]):	newidx = idx * 3	ourGroup =	[		input[newidx], 		input[newidx+1],		input[newidx+2]	]

I'm sure there's probably many different (and better) ways of doing this, but that's part of the reason I enjoy Python so much, the flexibility!

Photo by Artturi Jalli on Unsplash


This content originally appeared on Raymond Camden and was authored by Raymond Camden


Print Share Comment Cite Upload Translate Updates
APA

Raymond Camden | Sciencx (2022-12-05T18:00:00+00:00) Quirky Python Loop Thing. Retrieved from https://www.scien.cx/2022/12/05/quirky-python-loop-thing/

MLA
" » Quirky Python Loop Thing." Raymond Camden | Sciencx - Monday December 5, 2022, https://www.scien.cx/2022/12/05/quirky-python-loop-thing/
HARVARD
Raymond Camden | Sciencx Monday December 5, 2022 » Quirky Python Loop Thing., viewed ,<https://www.scien.cx/2022/12/05/quirky-python-loop-thing/>
VANCOUVER
Raymond Camden | Sciencx - » Quirky Python Loop Thing. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2022/12/05/quirky-python-loop-thing/
CHICAGO
" » Quirky Python Loop Thing." Raymond Camden | Sciencx - Accessed . https://www.scien.cx/2022/12/05/quirky-python-loop-thing/
IEEE
" » Quirky Python Loop Thing." Raymond Camden | Sciencx [Online]. Available: https://www.scien.cx/2022/12/05/quirky-python-loop-thing/. [Accessed: ]
rf:citation
» Quirky Python Loop Thing | Raymond Camden | Sciencx | https://www.scien.cx/2022/12/05/quirky-python-loop-thing/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.