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?

asked Mar 13, 2021 at 5:44
7
  • Please go through the intro tour, the help center and how to ask a good question 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. Commented Mar 13, 2021 at 5:45
  • check this stackoverflow.com/questions/15037226/… Commented Mar 13, 2021 at 5:47
  • thanks for advice, I have added my attempt at the problem Commented Mar 13, 2021 at 5:48
  • check this out : rextester.com/IVOR40775 Commented Mar 13, 2021 at 5:49
  • actually I want it so that elements don't repeat Commented Mar 13, 2021 at 5:50

2 Answers 2

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]]
answered Mar 13, 2021 at 5:52
Sign up to request clarification or add additional context in comments.

Comments

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

answered Mar 13, 2021 at 6:01

2 Comments

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.
Oh my bad. Itertools removes same objects but not sub items. Let me write a code myself to do this task

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.