9

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!!

JOSEPH Blessingh
  • 157
  • 2
  • 10

3 Answers3

11

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
Usman
  • 1,983
  • 15
  • 28
  • Yours looks promising. I will try and let you know. The thing is I am doing operations on List of lists. [[16777230, 0], [16777226, 1], [16777252, 2], [16777246, 0]]. So, kinda figuring the operations. – JOSEPH Blessingh Jul 08 '19 at 07:28
  • thanks! yes you can use the same approach for performing operations by using the index of outer `i` & inner `j`. – Usman Jul 08 '19 at 07:33
  • The problem is I am using delete operations to alter the list in between the loop. So the length of list of lists is creating issues for me – JOSEPH Blessingh Jul 08 '19 at 08:23
  • Hey there, I made it work. I used new_list = old_list.copy to create a copy of the old list. If I used new_list = old_list and made and made any changes, it was reflected in the old_list as well. So, I made it to work. Thank you so much. Ticked your answer as it is the most apt!! – JOSEPH Blessingh Jul 08 '19 at 15:17
  • 1
    okay. Great ! Appreciation for me (y) Thanks alot ! @JOSEPHBlessingh – Usman Jul 09 '19 at 07:31
4

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)
bharatk
  • 4,202
  • 5
  • 16
  • 30
2

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.

Xiidref
  • 1,456
  • 8
  • 20