0

I am new to stack overflow so please excuse me if I am not clear.

aim: to write a program that would remove the elements that are present in preceding nested lists

here is the list: gang=[[1], [7], [11], [13], [2, 4, 6, 8, 10, 12, 14], [3, 6, 9, 12, 15], [6, 12], [5, 10, 15]]

desired output: gang=[[1], [7], [11], [13], [2, 4, 6, 8, 10, 12, 14], [3, 6, 9, 15], [5, 10]]

attempt:

for i in range(1,len(gang)):
    elem=gang[i]
    for j in range(len(elem)):
        for hey in range(i):
            if elem[j] in gang[hey]:
                elem.pop(j)

explanation of the attempt:

1)to traverse through list gang, I used a for loop (range is 1,len(gang) bcs 0th element wont be changed as it is the first one in the list)

2)I have declared a variable, 'elem' which equates to gang[i] i.e. the elements of gang from gang[1] to gang[-1]

3)Now, to traverse through the elements of the elements i.e. the elements of each nested list, I have used another for loop

4)the last for loop (for hey in range(i)) is used so that I can confirm if elem[j] i.e. element of nested list, exists in preceding nested lists, and if the condition is satisfied elem[j] would be removed

expected output:

gang=[[1], [7], [11], [13], [2, 4, 6, 8, 10, 12, 14], [3, 6, 9, 15], [5, 10]]

output:

if elem[j] in gang[hey]:
IndexError: list index out of range

question: why is this error showing? potential fix for it? any better way to achieve aim?

  • Please go through the [intro tour](https://stackoverflow.com/tour), the [help center](https://stackoverflow.com/help) and [how to ask a good question](https://stackoverflow.com/help/how-to-ask) to see how this site works and to help you improve your current and future questions, which can help you get better answers. "Show me how to solve this coding problem?" is off-topic for Stack Overflow. You have to make an honest attempt at the solution, and then ask a *specific* question about your implementation. Stack Overflow is not intended to replace existing tutorials and documentation. – Prune Mar 13 '21 at 05:45
  • check this https://stackoverflow.com/questions/15037226/python-remove-duplicate-items-from-nested-list – Chathuranga Mar 13 '21 at 05:47
  • thanks for advice, I have added my attempt at the problem – Captian Pool Mar 13 '21 at 05:48
  • check this out : https://rextester.com/IVOR40775 – A l w a y s S u n n y Mar 13 '21 at 05:49
  • actually I want it so that elements don't repeat – Captian Pool Mar 13 '21 at 05:50
  • Shouldn't your output be this: [[1], [7], [11], [13], [2, 4, 6, 8, 10, 12, 14], [3, 9, 15], [], [5]]? – pakpe Mar 13 '21 at 06:02
  • @Chathuranga. That is not the same problem as this. – pakpe Mar 13 '21 at 06:14

2 Answers2

1

Here it is. You create a visited list, iterate backward through each sublist, and remove an element if it is already visited:

gang=[[1], [7], [11], [13], [2, 4, 6, 8, 10, 12, 14], [3, 6, 9, 12, 15], [6, 12], [5, 10, 15]]
visited = []
for lst in gang:
    for i in range(len(lst)-1, -1, -1):
        if lst[i] not in visited:
            visited.append(lst[i])
        else:
            lst.pop(i)
print(gang)
#[[1], [7], [11], [13], [2, 4, 6, 8, 10, 12, 14], [3, 9, 15], [], [5]]
pakpe
  • 5,391
  • 2
  • 8
  • 23
0

The library itertools is very helpful in this case

import itertools
gang=[[1], [7], [11], [13], [2, 4, 6, 8, 10, 12, 14], [3, 6, 9, 12, 15], [6, 12], [5, 10, 15]]
gang.sort()#Sorting the list
gang = list(k for k,_ in itertools.groupby(gang))#This removes repetition
print (gang)

Check it on a repl here https://repl.it/join/keztvujw-siddharthagraw2

Siddharth Agrawal
  • 2,960
  • 3
  • 16
  • 37
  • This code returns [[1], [2, 4, 6, 8, 10, 12, 14], [3, 6, 9, 12, 15], [5, 10, 15], [6, 12], [7], [11], [13]]. This is the same as the input list except that the sublists are shuffled. – pakpe Mar 13 '21 at 06:06
  • Oh my bad. Itertools removes same objects but not sub items. Let me write a code myself to do this task – Siddharth Agrawal Mar 13 '21 at 06:08