Avoiding multiple nested for-loops in python

0 votes

How to avoid multiple nested for-loops when one nested for-loop has range up to the current iteration of the outer for-loop? For example, consider the following code: This program returns a triplet from a list arr such that arr[i] - arr[j] = arr[j] - arr[k] = d and i<j<k.

d =3
arr = [1, 2, 4, 5, 7, 8, 10]
list1 = []

for biggest in range(0, len(arr)):
    for bigger in range(0, biggest):
        for big in range(0, bigger):
            if abs(arr[big] - arr[bigger]) == d and abs(arr[bigger] - arr[biggest]) == d:
                list1.append([arr[big], arr[bigger], arr[biggest]])
print(list1))

Are there any other alternatives to using multiple nested loops?

Sep 14, 2018 in Python by bug_seeker
• 15,520 points
16,131 views

2 answers to this question.

+1 vote

You can replace the three loops with:

from itertools import combinations

for big, bigger, biggest in combinations(range(0, len(arr)), 3):

You can replace all the code with:

print([t for t in combinations(arr, 3)
       if t[2] - t[1] == t[1] - t[0] == d])

Hope this helps!!

If you need to learn more about Python, It's recommended to join Python Programming course today.

Thanks!

answered Sep 14, 2018 by Priyaj
• 58,100 points
+1 vote
Instead of multi-loop, If you can categorize all the threads into a loop, you can easily go with the less complexity with the in python, and for the nested loop, it is total standing with the loop in between the loop.
answered Sep 15, 2018 by pedro67657
• 160 points

edited Sep 15, 2018 by Vardhan
Can you explain somewhat deeply using syntax bro please

Hii @Nikhil,

Example code:

d =3
arr = [1, 2, 4, 5, 7, 8, 10]
list1 = []

for biggest in range(0, len(arr)):
    for bigger in range(0, biggest):
        for big in range(0, bigger):
            if abs(arr[big] - arr[bigger]) == d and abs(arr[bigger] - arr[biggest]) == d:
                list1.append([arr[big], arr[bigger], arr[biggest]])
print(list1))

You can replace the three loops with:

from itertools import combinations

for big, bigger, biggest in combinations(range(0, len(arr)), 3):

You can replace all the code with:

print([t for t in combinations(arr, 3)
       if t[2] - t[1] == t[1] - t[0] == d])

Hey, @Nikhil,

Could you please make your query a bit clear that how you want to use syntax and where? Please post your workarounds.

Related Questions In Python

0 votes
1 answer

Python: nested 'for' loops

Could use itertools: >>> for comb in itertools.combinations_with_replacement(range(9, -1, ...READ MORE

answered Sep 3, 2018 in Python by Priyaj
• 58,100 points
977 views
0 votes
1 answer

How can I deal with python eggs for multiple platforms in one location?

Try virtualenv : http://pypi.python.org/pypi/virtualenv This helps you create isolated ...READ MORE

answered May 28, 2019 in Python by SDeb
• 13,300 points
868 views
+1 vote
1 answer

instead of using two for loops in python

This example might help: for x, y in ((a,b) ...READ MORE

answered Dec 24, 2019 in Python by Sumeir
1,134 views
+3 votes
5 answers

How to read multiple data files in python

Firstly we will import pandas to read ...READ MORE

answered Apr 6, 2018 in Python by DeepCoder786
• 1,720 points
15,110 views
0 votes
2 answers
+1 vote
2 answers

how can i count the items in a list?

Syntax :            list. count(value) Code: colors = ['red', 'green', ...READ MORE

answered Jul 7, 2019 in Python by Neha
• 330 points

edited Jul 8, 2019 by Kalgi 4,354 views
0 votes
1 answer
0 votes
1 answer

Avoiding multiple nested for-loops in python

You can replace the three loops with: from ...READ MORE

answered Sep 7, 2018 in Python by Priyaj
• 58,100 points
825 views
0 votes
1 answer

Avoiding multiple nested for-loops in python

You can replace the three loops with: from ...READ MORE

answered Sep 10, 2018 in Python by Priyaj
• 58,100 points
3,194 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP