from itertools import product

def horizontal():
    for x, y in product(range(20), range(17)):
        print 1 + sum(int(n) for n in grid[x][y: y + 4])

You should be using the sum function. Of course you can't if you shadow it with a variable, so I changed it to my_sum

Answer from John La Rooy on Stack Overflow
🌐
Reddit
reddit.com › r/learnprogramming › are there any alternatives to nested loops
r/learnprogramming on Reddit: Are there any alternatives to Nested loops
December 8, 2021 -

My mind blanks as how replace nested loops with a faster and perhaps a less wasteful way? I know they can’t always be replaced but when is a good time to replace them.

I figure this will come with time but does anyone have any general or specific tips, use cases or examples of how to replace loops or improve on this. I feel like this what’s turning good ideas into lame or unimpressive by speed issues.

Been practicing algorithms in c, JavaScript and python for about a year and I seem to constantly get slower solutions when doing leetcode for example. Self taught udemy, books and cs50x.

Ideally I like to refactor and improve a python script I created with interns and others that read 100,000 reviews and creates moods similarity recommendation engine that indexed and sorts by mood but it take 7-8 days to update it. But then I upload csv to database and works in fast. But had times where something went wrong and took 21 days to get a file. But before doing that I like to learn in general so I can make better decisions coding.

🌐
Reddit
reddit.com › r/learnpython › what is an alternative to nested loops?
r/learnpython on Reddit: What is an alternative to nested loops?
December 18, 2021 -

Given 2 lists:

list1 = [[1,2,3],[4,5,6],[7,8,9]]

list2 = []

Expected result:

list2 = [[1,4,7],[2,5,8],[3,6,9]]

I can do that by nesting For loops:

for i in range(0,len(list1[0])):
    temp = []
    for element in list1:
        temp.append(element[i])
    list2.append(temp)
    i+=1

But this is very memory inefficient, so sometimes this procedure can't even return a result. Is there any alternative to this?

🌐
Quora
quora.com › What-are-some-alternatives-of-nested-for-loops
What are some alternatives of nested for loops? - Quora
Answer (1 of 3): You can just simply use normal functions like this. [code]for(int x=1;x
🌐
Python Forum
python-forum.io › thread-39604.html
reduce nested for-loops
Hi, i have lots of 'search'-loops which looks like this: for b in a.list_of_bs: for c in b.list_of_cs: for d in c.list_of_ds: do_something()This is a clear code smell. So how co...
🌐
Medium
medium.com › python-pandemonium › never-write-for-loops-again-91a5a4c84baf
You (Probably) Don’t Need For-Loops | by Daw-Ran Liou | Python Pandemonium | Medium
March 28, 2017 - THIS IS HARD TO READ. The problem I found in this code is that it is mixing the administrative logic (the with, try-except) with the business logic (the for, if) by giving them the indentation ubiquitously. If you are disciplined about using indentation only for administrative logic, your core business logic would stand out immediately. “Flat is better than nested” — The Zen of Python
🌐
Narkive
tutor.python.narkive.com › JbJOw6Nh › alternative-to-nested-loops
[Tutor] Alternative to nested loops
tutor@python.org · Discussion: [Tutor] Alternative to nested loops · Steve Nelson · 2006-03-19 21:05:37 UTC · Permalink Hi All, foo [[1, 2, 3], [1, 2, 3], [1, 2, 3]] ... for b in c: ... print b ... 1 2 3 1 2 3 1 2 3 Using a list comprehension, as it seemed to me like I was saying: b for c in foo, but I can't see how to do this.
Find elsewhere
Top answer
1 of 4
3

You can reduce the number of lines of code and make it easier to understand like this:

tasks_id = [task in tasks if task["id"] == entry["item_id"]]
folders_dict = dict()
for folder in folders:
    folders_dict[folder["id"]] = folder

for task in tasks_id:
    if task["parent_id"] in folders_dict.keys() and folder_dict[task["parent_id"]] in forbiddenFolders:
        body_of_email +=( "Using a task within a folder that is not permiited " + forbiddenFolder + "\r\n" )
2 of 4
2

What you're trying to do here is look up an item (task, folder) based on the id. Python's dictionaries provide an easy way to do this. You will only save if you'll be doing the searches several times (e.g. if there are many tasks with the same id, or if you'll be running the function several times).

Additionally, for forbiddenFolders you just have a list of names (you're not looking up an item, you're just checking if it's present) for which Python's sets are suitable.

Anyway, here is how you build the dictionaries and sets:

tasks_dict = dict((task['id'], task) for task in tasks)
folders_dict = dict((folder['id'], folder) for folder in folders)
forbidden_folders_set = set(forbiddenFolders)

Now, task = tasks_dict[id] is a task such that task['id'] == id, and similarly for folders, so you can replace the loops above with these expressions. The set doesn't allow this, but it allows you to check for presence with folder in forbidden_folders_set.

(Bear in mind that each of those dict(...) operations may take longer than running through one of the for loops above, but they are an investment for faster lookup in future.)

if entry['item_id'] in tasks_dict:
    task = tasks_dict[entry['item_id']]
    if task['parent_id'] in folders_dict:
        folder = folders_dict[task['parent_id']]
        if folder in forbidden_folders_set:
            body_of_email += ...

The x in y and ..._dict[x] operations above are very efficient.

🌐
Stack Overflow
stackoverflow.com › questions › 23562572 › alternative-to-nested-for-loop
python - Alternative to nested for loop - Stack Overflow
Bash is really slow while processing for loops and I had the idea of using python for this approach. python - << EOF posList=posString.split() endList=endString.split() startList=startString.split() for j, val2 in enumerate(posList): for i, val1 in enumerate(startList): if val1 >= val2 and endList[i] <= val2: print "true", val2 else: print "false", val2 EOF · I have three strings as input (position, start, end) and split them into lists. With the two nested loops I iterate over the bigger position file and then over the star/end file.
🌐
Reddit
reddit.com › r/learnpython › alternative to nested loops
r/learnpython on Reddit: Alternative to nested loops
March 13, 2021 -

I wrote a small script to solve a puzzle (from West of Loathing, FWIW): You have three knobs that increase a value by a given amount:

  • Knob A increases the value by 411

  • Knob B increases the value by 295

  • Knob C increases the value by 161

You can turn each knob up to 8 times, and need to arrive at a value of 3200 (starting from 0)

My script looks like this:

import sys

for a in range(0,9):
    for b in range(0,9):
        for c in range(0,9):
            print(f"a={a} b={b} c={c}")
            if(a*411+b*295+c*161) == 3200:
                print("that's it!")
                sys.exit()

I love it not, for it is clumsy. How could I have done this better?

Top answer
1 of 4
2

Since I cannot create N nested for loops, what is the alternative?

Recursion!

Have a function taking extra arguments, such as the number of terms to sum and the target number, then in its implementation call itself again with one fewer term.

Your stopping condition is when the number of terms to sum is zero. In that case, if the target number to reach is zero, it means you found a valid sum. If it's non-zero, it means you didn't. (Similarly, you could do the last check at 1 term left, checking whether you can pick a final number to match it.)

Since you only need to find sets of distinct numbers that sum up to one, you can assume x > y > z > a > b (or the opposite ordering), to make sure you're not finding the same sequence over and over again, just in a different order.

Also, iterating down from the limit means the reciprocals will grow as you proceed in the iteration. Which also means you can stop looking once the sum goes past one (or the target gets negative), which should help you quickly prune loops that won't ever yield new values.

Finally, Python also supports fractions, which means you can make these calculations with exact precision, without worrying about the rounding issues of floats.

Putting it all together:

from fractions import Fraction

def reciprocal_sums(n=5, limit=30, target=1, partial=()):
    if n == 0:
        if target == 0:
            yield partial
        return
    for i in range(limit, 0, -1):
        new_target = target - Fraction(1, i)
        if new_target < 0:
            return
        yield from reciprocal_sums(
            n - 1, i - 1, new_target, partial + (i,))

Testing it for n=5 (default):

>>> list(reciprocal_sums())
[(30, 20, 12, 3, 2),
 (30, 20, 6, 4, 2),
 (30, 10, 6, 5, 2),
 (28, 21, 12, 3, 2),
 (28, 21, 6, 4, 2),
 (28, 14, 7, 4, 2),
 (24, 12, 8, 4, 2),
 (20, 12, 6, 5, 2),
 (20, 6, 5, 4, 3),
 (18, 12, 9, 4, 2),
 (15, 12, 10, 4, 2)]

For n=4:

>>> list(reciprocal_sums(4))
[(24, 8, 3, 2),
 (20, 5, 4, 2),
 (18, 9, 3, 2),
 (15, 10, 3, 2),
 (12, 6, 4, 2)]

And n=6:

>>> list(reciprocal_sums(6))
[(30, 28, 21, 20, 3, 2),
 (30, 24, 20, 8, 4, 2),
 (30, 24, 10, 8, 5, 2),
 (30, 20, 18, 9, 4, 2),
 (30, 20, 15, 10, 4, 2),
 (30, 18, 10, 9, 5, 2),
 (30, 12, 10, 5, 4, 3),
 (28, 24, 21, 8, 4, 2),
 (28, 21, 20, 6, 5, 2),
 (28, 21, 18, 9, 4, 2),
 (28, 21, 15, 10, 4, 2),
 (28, 20, 14, 7, 5, 2),
 (28, 14, 12, 7, 6, 2),
 (28, 14, 7, 6, 4, 3),
 (24, 20, 12, 8, 5, 2),
 (24, 20, 8, 5, 4, 3),
 (24, 18, 9, 8, 6, 2),
 (24, 15, 10, 8, 6, 2),
 (24, 12, 8, 6, 4, 3),
 (20, 18, 12, 9, 5, 2),
 (20, 18, 9, 5, 4, 3),
 (20, 15, 12, 10, 5, 2),
 (20, 15, 10, 5, 4, 3),
 (18, 15, 10, 9, 6, 2),
 (18, 12, 9, 6, 4, 3),
 (15, 12, 10, 6, 4, 3)]

This solution is pretty fast. Running on a Snapdragon 845 ARM CPU:

%timeit list(reciprocal_sums(4)) 
365 ms ± 5.74 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)

%timeit list(reciprocal_sums(5))
1.94 s ± 8.93 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)

%timeit list(reciprocal_sums(6))
8.26 s ± 56.1 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)

The ordering (lowering the limit at each level) together with pruning the last levels after going above the target will make this solution much faster than ones that evaluate all possible permutations or combinations.

2 of 4
0

itertools.product(list(range(N)),repeat=N)

maybe what you want

🌐
Reddit
reddit.com › r/learnpython › how to make nested for loops run faster
r/learnpython on Reddit: How to make nested for loops run faster
February 27, 2023 -

Hi, i'm currently in situation where my function needs run with multiple variations and at this point it takes a two days to calculate what I need. Multiprocessing not helping. So I want to ask how to improve nested for loops that currently structure looks something like this?

for i in range(50):
    for j in range(50):
        for k in range(50):
            for m in range(50):
                func(i, j, k, m, big_list)
Top answer
1 of 26
116
The only ways are: Parallelism (e.g. multiprocessing) Early returns Optimization elsewhere Here's the thing, if you have 4 nested for loops of 50 each. That's 504 = 6,250,000. Instead of 4 nested loops, you could loop over all 6 million items in a single for loop, but that probably won't significantly improve your runtime. No matter how you spin it, 6 million is just a lot of items, as it turns out. I mentioned optimization. That will help each iteration run faster, but that's still 6 million items. Parallelism is usually a good way to handle these things. Feel free to post your actual code - perhaps it is possible, parallelism can be tricky to get right - but otherwise I'll have to take your word that it won't help here. So all we're left with is early returns. Meaning, is there any way to break out of a loop or otherwise not run all 50 iterations of some level? The higher-level and earlier the better. That would effectively reduce the number of iterations. Edit: Math is hard Edit2: Actually, there's one more way. 4. Clever algorithms The root problem is 6 million iterations. Sometimes, clever algorithms can change the entire structure of your program, and remove the need for nested loops. My favorite category is called memoization (not a typo). Essentially, some problems look like you need to loop over a list in nested loops, but by storing the best answer at each stage, you can refer back to a previous stage and simply choose or add etc. That means you only need to loop over the list once instead once per item per item etc. Such clever algorithms are not always possible. But when they are, you feel like a god :)
2 of 26
35
So I want to ask how to improve nested for loops that currently structure looks something like this? The answer is "don't structure it like that." The only way to make that go faster is to do less - you're calling func six million times (504), but if you can figure out that func(x, x, y, y) == func(y, y, x, x) for some values x and y, or a similar property, then you can cut your runtime in half by not evaluating both combinations of values. Ultimately your computer is as fast as it is and 6250000 is as large a number as it is. There's not a drop-in way to improve that runtime; if there were, that would just be how the language worked and everything would be that fast.
🌐
CopyProgramming
copyprogramming.com › howto › alternative-to-nested-for-loops-python-code-example
Python: Python Code Example for Avoiding Nested For Loops
May 13, 2023 - Alternative (faster) war to 3 nested for loop python, Equivalent to nested for-loops in a generator expression. For example, product(A, B) returns the same as ((x,y) for x in A for y in B). The nested loops cycle like an odometer with the rightmost element advancing on every iteration.