0

I have a problem here when I want to remove duplicate in a list that has a nested list, how can I remove the duplicate value from list? What I got here from my script, it can remove a duplicate, but the nested list has a different result from what I expect.

This is my script:

# initializing list 
result = []
hasil = []
sam_list = [[11, 17, 11, 13, 13, 15, 16, 11], [4, 7, 11, 34, 4, 7, 11, 6], [1, 6, 11, 13, 13, 4, 1, 6]]

for item in sam_list:
    print("START")
    for x in item:
        print(x, result)
        if x not in result:
            print("NOT IN")
            result.append(x)
    hasil.append(result)

Result:

[[11, 17, 13, 15, 16, 4, 7, 34, 6, 1], [11, 17, 13, 15, 16, 4, 7, 34, 6, 1], [11, 17, 13, 15, 16, 4, 7, 34, 6, 1]]

Expected Result:

[[11, 17, 13, 15, 16], [4, 7, 11, 34, 6], [1, 6, 11, 13, 4]]
0

3 Answers 3

1

Almost there, you just have to reset the result list at every step

for item in sam_list:
   print("START")
   for x in item:
      print(x, result)
      if x not in result:
         print("NOT IN")
         result.append(x)
   hasil.append(result)
   result = []
Sign up to request clarification or add additional context in comments.

Comments

0

You need to initialize result = [] inside the loop:

sam_list = [[11, 17, 11, 13, 13, 15, 16, 11], [4, 7, 11, 34, 4, 7, 11, 6], [1, 6, 11, 13, 13, 4, 1, 6]]
hasil = []

for item in sam_list:
    result = []
    print("START")
    for x in item:
        print(x, result)
        if x not in result:
            print("NOT IN")
            result.append(x)
    hasil.append(result)

If you don't mind order:

original = [[11, 17, 11, 13, 13, 15, 16, 11], [4, 7, 11, 34, 4, 7, 11, 6], [1, 6, 11, 13, 13, 4, 1, 6]]

[list(set(each)) for each in original]

1 Comment

0

You can use set. It automatically removes any duplicates, then convert it to a list again if you want.

sam_list = [[11, 17, 11, 13, 13, 15, 16, 11], [4, 7, 11, 34, 4, 7, 11, 6], [1, 6, 11, 13, 13, 4, 1, 6]]
set1=set(sam_list[0])
set2=set(sam_list[1])
set3=set(sam_list[2])

result = [list(set1),list(set2),list(set3)]
print(result)

the output is: [[11, 13, 15, 16, 17], [34, 4, 6, 7, 11], [1, 4, 6, 11, 13]]

Comments

Your Answer

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.