0

I have to decrease my range in every loop end. here is an example code:

for i in range(len(array)):
    if (array[0][5] == array[i][5]):
       array2.append(array[0])
       array3.append(array[i])
       
       array.remove(array[i])
       array.remove(array[0])

this throws an error that list index out of range so I tried to add i-=1 in every remove functions end but it doesn't fix it.

I couldnt fixed it so I tried to fix this problem in another way.

n = len(array) # maybe it could be len(array) -1 for the index problem but I also tried that it didnt work.
k = len(array)-1
for n in range(n, 0, -2):
    if (array[0][5] == array[n][5]):
       array2.append(array[0])
       array3.append(array[n])
       
       array[k] = array[n]
       array[k-1] = array[0]
       k -= 2

I thought that if I do that, I shouldn't compare the last 2 values after the loop but the error is the same error : list index out of range

Solving this problem with NOT starting loop from the end is my first priority.

I want to match 2 values from 2 different arrays when arrays 2's elements equals to each other and then I have to out them off from the list because when I match 2 values I don't need them anymore. But there is a problem right there which is when I remove the element from the list, I can not reach all the values in the list because of the iteration still during.

array1 = [0,1],[0,2],[0,2],[0,2],[0,1],[0,3]
array2 = [0,1],[0,1],[0,1],[0,2],[0,2],[0,3]
for i in range(len(array1)):
   for j in range(len(array2)):
      if(array1[i][1] == array2[j][1]):
         temp_arr2.append(array1[i])
         temp_arr1.append(array2[j])
         array1.remove(array1[i])
         array2.remove(array2[j])
maddog
  • 29
  • 4
  • 3
    What is `n`, and how is it related to `array`? – Ben Grossmann Feb 27 '22 at 23:08
  • It would probably be easier if you could just explain what you're trying to do with array, array2, and array3 – Ben Grossmann Feb 27 '22 at 23:09
  • 1
    Does this answer your question? [Strange result when removing item from a list while iterating over it](https://stackoverflow.com/questions/6260089/strange-result-when-removing-item-from-a-list-while-iterating-over-it) – Grismar Feb 27 '22 at 23:30
  • n should be i, I just fixed it. ARRAY is an 2 dimensional array, array2 is some part of the ARRAY, array3 is also some part of the ARRAY and I want to compare values in ARRAY's elements because I want to seperate array 2 parts. This comparation allows me to find difference between 2 arrays so I append some kind of values to array2 and some similar values to array3. But there is a problem right there. Problem is: if I find the similar values in the array, ı have to out them off. if I still put them inside the array after the comparation, can match another values that I dont want to. – maddog Feb 27 '22 at 23:31
  • @grismar I already tried that you have mentioned above just check it I edited. – maddog Feb 27 '22 at 23:41

1 Answers1

0

The issue is that you may not be able to control the iteration of the variable i when using it in a for loop. Your instincts are good in attempting to modify i by 1 for each remove() call, and the missing piece of the puzzle is to use a while loop instead of a for loop.

Here is code that does what your question asks using sample data for array:

        array = [
            [0,0,0,0,0,99],
            [1,0,0,0,0,33],
            [2,0,0,0,0,66],
            [3,0,0,0,0,99],
            [4,0,0,0,0,33],
            [5,0,0,0,0,66],
            [6,0,0,0,0,99],
            [7,0,0,0,0,33],
            [8,0,0,0,0,66],
            [9,0,0,0,0,99],
            [10,0,0,0,0,33]
        ]
        array2 = []
        array3 = []
        i = 0
        while i < len(array):
            if (array[0][5] == array[i][5]):
                array2.append(array[0])
                array3.append(array[i])

                array.remove(array[i])
                array.remove(array[0])
                i += 2
            i += 1
        print(f"array {array}")
        print(f"array2 {array2}")
        print(f"array3 {array3}")

Here is sample output:

array [[3, 0, 0, 0, 0, 99], [4, 0, 0, 0, 0, 33], [6, 0, 0, 0, 0, 99], [7, 0, 0, 0, 0, 33], [8, 0, 0, 0, 0, 66], [9, 0, 0, 0, 0, 99], [10, 0, 0, 0, 0, 33]]
array2 [[0, 0, 0, 0, 0, 99], [2, 0, 0, 0, 0, 66]]
array3 [[0, 0, 0, 0, 0, 99], [5, 0, 0, 0, 0, 66]]

UPDATE: Here is an updated answer based on the updated question.

It seems as though the main concern is identifying and collecting lists contained in array1 and array2 that have matching values in a particular index. The end state of array1 and array2 does not matter.

With this in mind, I suggest the following approach which uses a Python dict to identifying matches more efficiently than nested for loops, and which will create the required lists of matches. Note that it will not modify the original lists (array1 and array2).

        array1 = [[100,1],[100,2],[100,2],[100,2],[100,1],[100,3]]
        array2 = [[200,1],[200,1],[200,1],[200,2],[200,2],[200,3]]
        dict1 = {}
        for i in range(len(array1)):
            key = array1[i][1]
            if key not in dict1:
                dict1[key] = []
            dict1[key].append(i)
        temp_arr1 = []
        temp_arr2 = []
        for j in range(len(array2)):
            key = array2[j][1]
            if key in dict1:
                i = dict1[key][0]
                temp_arr2.append(array1[i])
                temp_arr1.append(array2[j])
                dict1.pop(key)
        print(temp_arr1)
        print(temp_arr2)

Here are the outputs:

[[200, 1], [200, 2], [200, 3]]
[[100, 1], [100, 2], [100, 3]]

As you can see, the first output row (temp_arr1) contains lists that were originally in array2 and the second output row (temp_arr2) contains lists that were originally in array1. This matches the updated sample code in the question.

constantstranger
  • 9,176
  • 2
  • 5
  • 19
  • when you increase i by 2 in if statement, end of the if statement you are not able to access array's [0] and [1] indexes in another loop so you can not compare those indexes with others. – maddog Feb 28 '22 at 02:06
  • @maddog, that's true, and before I update my answer, let me ask: in your original code, when you write `array.remove(array[0])`, is it your intention that on the next loop iteration the `==` comparison will be made between the new `array[0]` (i.e., what was previously `array[1]`) and `array[i]`? – constantstranger Feb 28 '22 at 02:30
  • @maddog, another question to ensure I am fully understanding your goal: given the sample `array` in my answer, what is the desired end result for `array2` and `array3`? – constantstranger Feb 28 '22 at 02:39
  • in my original code, 3 more comparation that I need first if array[0][5] == array[i][5] second if array1[0][5] == array1[0][5] etc. but these comparations not important at all. If I can handle with this >remove and compare> problem, its okay for me – maddog Feb 28 '22 at 02:51
  • I mentioned above before; ARRAY is an 2 dimensional array, array2 is some part of the ARRAY, array3 is also some part of the ARRAY and I want to compare values in ARRAY's elements because I want to seperate array 2 parts. This comparation allows me to find difference between 2 arrays so I append some kind of values to array2 and some similar values to array3. But there is a problem right there. Problem is: if I find the similar values in the array, ı have to out them off. if I still put them inside the array after the comparation, can match another values that I dont want to. – maddog Feb 28 '22 at 02:55
  • 1
    I'm still not sure I understand your exact goal. Can you give a concrete example please for `array` and for `array2` and `array3`? I think if you can provide that, I should then be able to refine my answer to do what you need. – constantstranger Feb 28 '22 at 03:05
  • I want to match 2 values from 2 different arrays when arrays 2's elements equals to each other and then I have to out them off from the list because when I match 2 values I don't need them anymore. But there is a problem right there which is when I remove the element from the list, I can not reach all the values in the list because of the iteration still during. – maddog Mar 01 '22 at 12:59
  • ``` temp_arr1 = [] temp_arr2 = [] array1 = [0,1],[0,2],[0,2],[0,2],[0,1],[0,3] array2 = [0,1],[0,1],[0,1],[0,2],[0,2],[0,3] for i in range(len(array1)): for j in range(len(array2)): if(array1[i][1] == array2[j][1]): temp_arr2.append(array1[i]) temp_arr1.append(array2[j]) array1.remove(array1[i]) array2.remove(array2[j]) ``` – maddog Mar 01 '22 at 13:00
  • take a look at it ı just edited again my question – maddog Mar 01 '22 at 13:01
  • OK, your new example is helpful. What would `array1` and `array2` look like after the loops have executed? For example, do you want them both to be empty (since both contain the same elements, just in different sequence and with a different number of copies)? Or, do you want `array1` to contain `[0,2]` (since it started with 3 copies and `array2` started with only 2 copies) and `array2` to contain `[0,1]` (since it started with 3 copies and `array1` started with only 2 copies)? – constantstranger Mar 01 '22 at 13:25
  • it doesn't matter what array1 and array2 looks like its should just be empty after the iteration because when I match 2 values ı dont need them anymore, I'm appending another 2 temp array and return them. I can concatenate them later so it doesn't matter. If they same values , first one will be added – maddog Mar 01 '22 at 13:34
  • Got it. Please see my updated answer. – constantstranger Mar 01 '22 at 14:12
  • @maddog Do you need any more help with this? – constantstranger Mar 02 '22 at 16:59