0

My input data is

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

My expected output is:

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

With the help of How to compare each item in a list with the rest, only once? currently my snippet looks like

mylist = [[1, 2], [3, 4], [5, 6], [1, 2, 7], [8, 2], [9, 5]]
result = list()

for i in range(len(mylist)):
    result.append(mylist[i])
    for j in range(i + 1, len(mylist)):
        if set(mylist[i]) & set(mylist[j]):
            result.append(mylist[j])
            mylist.remove(mylist[j])

print(result)

However, It is throwing error IndexError: list index out of range. I guess this is because I am trying to remove items from a list while iterating.

So I checked How to remove items from a list while iterating?. It suggested using slice or itertools. It also gave a code snippet which I found much more readable.

temp = []
while somelist:
    x = somelist.pop()
    if not determine(x):
        temp.append(x)
while temp:
    somelist.append(templist.pop())

However, I could not figure out how it might work. Any idea?

Update 1

Snippet:

mylist = [[1, 2], [3, 4], [5, 6], [1, 2, 7], [8, 2], [9, 5]]
result = list()

for i in range(len(mylist)):
    result.append(mylist[i])
    for j in range(i + 1, len(mylist)):
        if set(mylist[i]) & set(mylist[j]):
            result.append(mylist[j])
            # mylist.remove(mylist[j])

print(result)

Output:

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

I do not want [1, 2, 7], [8, 2], [8, 2], [9, 5] in the result so I trying to use mylist.remove(mylist[j]), which I could not figure out how to do.

Ahmad Ismail
  • 11,636
  • 6
  • 52
  • 87
  • How can u use this list for testing? There aren't any repeated values in this list. – Sushil Sep 30 '20 at 15:23
  • Does [this](https://stackoverflow.com/questions/21065347/remove-duplicates-in-two-dimensional-array-while-maintaining-sequence) answer your question? – Equinox Sep 30 '20 at 15:31
  • 1
    I don't get the logic by which you rearrange your list. – Patrick Artner Sep 30 '20 at 15:33
  • @venky__ let me check it out. – Ahmad Ismail Sep 30 '20 at 15:44
  • @Sushil I am comparing them as set using `set(mylist[i]) & set(mylist[j])` – Ahmad Ismail Sep 30 '20 at 15:45
  • @PatrickArtner the logic is - take the first element and append in the result list. Then compare `set(mylist[i]) & set(mylist[j])` and if match append in the result list, and also remove the element from `mylist[j]`. – Ahmad Ismail Sep 30 '20 at 15:48
  • @PatrickArtner Please let me rephrase. I am trying to compare each element of the list with rest of the elements. If match found, I am trying to add it to the result list and then remove the matched elements from the list. – Ahmad Ismail Sep 30 '20 at 15:53
  • @venky__ No, it does not answer my question. – Ahmad Ismail Sep 30 '20 at 15:54
  • I have updated the question to make it a little more clear. – Ahmad Ismail Sep 30 '20 at 16:01
  • If u don't want ```[1, 2, 7], [8, 2], [8, 2], [9, 5]``` in the ```result```, then y r u removing it from ```mylist```? – Sushil Sep 30 '20 at 16:49
  • @Sushil Given `mylist` is the input, If I do not remove the data which I do not want to duplicate from `mylist` it gives duplicate results in output which is `result` – Ahmad Ismail Sep 30 '20 at 16:58
  • @Sushil please let me rephrase. In my case, there is data in input which I am iterating over and over. If I do not remove the data from the input when I am moving it to the result, then it gets duplicated. – Ahmad Ismail Sep 30 '20 at 17:14
  • @blueray Check if my answer satisfies ur requirement. – Sushil Oct 01 '20 at 07:08
  • @blueray Thx for accepting my answer as the best answer. Could u also upvote my answer? – Sushil Oct 01 '20 at 07:41

2 Answers2

1

Ok...So from ur ques, I understand that u wanna remove the repeated values from result. I tried to complete this task using list.remove, but I couldn't. So I have done it in a different way. Instead of using list.remove, add these lines to ur code:

import copy
curr_lst = []

resultdup = copy.deepcopy(result)

for x in range(len(resultdup)):
    curr_lst = resultdup[x]
    for y in range(x+1,len(resultdup)):
        if curr_lst == resultdup[y]:
            indices = [i for i, x in enumerate(result) if x == curr_lst]
            for x in indices[1:]:
                del result[x]

print(result)

Output:

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

Hope that this helps u.

Sushil
  • 5,440
  • 1
  • 8
  • 26
1

OP Here, I could not find the answer of the question I originally asked. However, I found an alternative way (though it might not be as optimal as removing the element from mylist[j]) to achieve the result I was expecting.

The code I am currently using is:

mylist = [[1, 2], [3, 4], [5, 6], [1, 2, 7], [8, 2], [9, 5]]
temp_result = list()

for i in range(len(mylist)):
    temp_result.append(mylist[i])
    for j in range(i + 1, len(mylist)):
        if (set(mylist[i]) & set(mylist[j])):
            temp_result.append(mylist[j])

result = []
for elem in temp_result:
    if elem not in result:
        result.append(elem)

print(result)
Ahmad Ismail
  • 11,636
  • 6
  • 52
  • 87