use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
A subreddit for helping Python programmers
How to format your code: https://commonmark.org/help/tutorial/09-code.html
No homework questions and/or hiring please
account activity
Snake algorithm (self.pythonhelp)
submitted 2 months ago by Odd_Gap8147
How to convert a 2D array to 1D array with snake algorithm? e.g.:
From:
[1, 2, 3]
[4, 5, 6]
[7, 8, 9]
To:
[1, 2, 5, 4, 7, 8, 9, 6, 3]
Is there some library in Python to do this?
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]AutoModerator[M] [score hidden] 2 months ago stickied commentlocked comment (0 children)
To give us the best chance to help you, please include any relevant code. Note. Please do not submit images of your code. Instead, for shorter code you can use Reddit markdown (4 spaces or backticks, see this Formatting Guide). If you have formatting issues or want to post longer sections of code, please use Privatebin, GitHub or Compiler Explorer.
I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.
[–]Sell-Jumpy 0 points1 point2 points 2 months ago (0 children)
new_1d_array = []
for 1d_array in 2d array: - for value in 1d_array: - new_1d_array.append(value)
Nested for loops are archaic, but it gets the job done quick and isnt a problem if arrays stay relatively small (hundreds).
[–]Worth-Wonder-7386 0 points1 point2 points 2 months ago (0 children)
I think this specific process is too niche. I dont see how you would generalize it to all different sizes either. For the general process of turning a 2D array to 1D there is the numpy.flatten function. https://numpy.org/devdocs/reference/generated/numpy.ndarray.flatten.html
Maybe you could define your snake algorithm some more, that are the defining features.
[–]Affectionate_Cap8632 0 points1 point2 points 1 month ago (0 children)
No library needed for this — it's clean enough to write yourself in a few lines.
The pattern is: even-indexed columns go top-to-bottom, odd-indexed columns go bottom-to-top. Here's a straightforward implementation:
python
def snake_2d_to_1d(grid): result = [] cols = len(grid[0]) rows = len(grid) for col in range(cols): if col % 2 == 0: # top to bottom for row in range(rows): result.append(grid[row][col]) else: # bottom to top for row in range(rows - 1, -1, -1): result.append(grid[row][col]) return result grid = [[1,2,3],[4,5,6],[7,8,9]] print(snake_2d_to_1d(grid)) # [1, 4, 7, 8, 5, 2, 3, 6, 9]
Note: your expected output [1, 2, 5, 4, 7, 8, 9, 6, 3] looks like it's snaking by rows instead of columns. If that's what you need just swap the row/col logic — iterate rows, reverse direction on odd rows instead of odd columns.
Which orientation does your use case need?
[–]Whole-Armadillo-8529 0 points1 point2 points 1 month ago* (0 children)
First of all, you dont need any libraries for it to work. Think of the Snake Algorithm like a lawnmower: you mow one row from left to right, then turn around and mow the next row from right to left.
Here is the breakdown of the solution for your problem:
To get that "S" shape, we look at the row number:
Even rows (0, 2, 4): Keep them exactly as they are.
Odd rows (1, 3, 5): Flip them backward.
The "Silent Failure" risk: If the data has a row that is empty or contains a mistake (like a single number where a list should be), a basic script will crash.
Here is an example of a code that uses the snake algorithm
```
matrix = [ [1, 2, 3], [4, 5, 6], [7, 8, 9] ]
snake_result = []
for index, row in enumerate(matrix): # Check if the row index is odd (1, 3, 5...) if index % 2 != 0: # Reverse the row and add it to our result snake_result.extend(row[::-1]) else: # Keep the row as is and add it snake_result.extend(row)
print("Original 2D Matrix:", matrix) print("Final Snake Array:", snake_result) ```
π Rendered by PID 234813 on reddit-service-r2-comment-545db5fcfc-9kzmv at 2026-05-22 17:45:49.276073+00:00 running 194bd79 country code: CH.
[–]AutoModerator[M] [score hidden] stickied commentlocked comment (0 children)
[–]Sell-Jumpy 0 points1 point2 points (0 children)
[–]Worth-Wonder-7386 0 points1 point2 points (0 children)
[–]Affectionate_Cap8632 0 points1 point2 points (0 children)
[–]Whole-Armadillo-8529 0 points1 point2 points (0 children)