Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to run a nested loop in python inside list such that the outer loop starts from the next element of the list always and so on

I have a list something like:

[[16777230, 0], [16777226, 1], [16777252, 2], [16777246, 0]]

I want to make a loop inside a loop(nested loop) for my operation in python such that the inner loop will always start from the next element of the outer loop.

For e.g., the outer loop will traverse all the elements of the list from index 0 to 3 in each iteration. But the inner loop will start from index 1 and end at index 3 in the first iteration of the outer loop. Then, in the second iteration of the outer loop, the inner loop should traverse index 2 to index 3. And so on... The last iteration of the outer loop should make the inner loop traverse from index n to index n, basically only the last element, in this case index 3 to index 3.

The problem is I am deleting the elements of list while I traverse. So, it is creating issues like list index out of range while using range function to traverse.

How to construct these inner and outer loops?

I tried this, but doesn't seem to work:

for sub_list1 in yx:
    index_sl1 = yx.index(sub_list1)
    for sub_list2 in yx[index_sl1+1:]:
        Operations...

Help would be appreciated. Thank you!!

like image 930
JOSEPH Blessingh Avatar asked Jul 08 '19 07:07

JOSEPH Blessingh


3 Answers

Try this code !

For each iteration of outer loop, you need to iterate the inner loop from 1 increment to the range of list.

Code :

arr = [1,5,2,0,4,2,7]
for i in range(0,len(arr)):
    print("Iteration # : ", i+1)
    for j in range(i+1,len(arr)):
        print("Outer loop value : " , arr[i] , " Inner loop value : " , arr[j])

Output :

Iteration # :  1                                                                                                       
Outer loop value :  1  Inner loop value :  5                                                                           
Outer loop value :  1  Inner loop value :  2                                                                           
Outer loop value :  1  Inner loop value :  0                                                                           
Outer loop value :  1  Inner loop value :  4                                                                           
Outer loop value :  1  Inner loop value :  2                                                                           
Outer loop value :  1  Inner loop value :  7                                                                           
Iteration # :  2                                                                                                       
Outer loop value :  5  Inner loop value :  2                                                                           
Outer loop value :  5  Inner loop value :  0                                                                           
Outer loop value :  5  Inner loop value :  4                                                                           
Outer loop value :  5  Inner loop value :  2                                                                           
Outer loop value :  5  Inner loop value :  7                                                                           
Iteration # :  3                                                                                                       
Outer loop value :  2  Inner loop value :  0                                                                           
Outer loop value :  2  Inner loop value :  4                                                                           
Outer loop value :  2  Inner loop value :  2                                                                           
Outer loop value :  2  Inner loop value :  7                                                                           
Iteration # :  4                                                                                                       
Outer loop value :  0  Inner loop value :  4                                                                           
Outer loop value :  0  Inner loop value :  2                                                                           
Outer loop value :  0  Inner loop value :  7                                                                           
Iteration # :  5                                                                                                       
Outer loop value :  4  Inner loop value :  2                                                                           
Outer loop value :  4  Inner loop value :  7                                                                           
Iteration # :  6                                                                                                       
Outer loop value :  2  Inner loop value :  7                                                                           
Iteration # :  7
like image 149
Usman Avatar answered Oct 20 '22 19:10

Usman


The enumerate() method adds counter to an iterable and returns it (the enumerate object).

yx = [1,5,2,0,4,2,7]

for index,sub_list1 in enumerate(yx):
    for sub_list2 in yx[index+1:]:
        print(sub_list2)
like image 4
bharatk Avatar answered Oct 20 '22 18:10

bharatk


You should try to iterate with indexes directly instead of elements it would be easier to start from the nex position in your list :

your_list = [1,5,2,0,4,2,7]
for index in range(len(your_list)):
    element_outer_loop = your_list[index]
    for index2 in range(index+1, len(your_list)):
        element_inner_loop = your_list[index2]

In the code that you have done you get bad result because your list contains multiple time a same value (2 for example) and when you call the index(sub_list1) it will return the first corresponding elements so it will be good for the first 2 but for the next it will return the position of the first.

like image 2
Xiidref Avatar answered Oct 20 '22 18:10

Xiidref