Search code examples
pythonlistsetmutable

How can I check if there is a duplicate list in a list of lists?


I have a list RESULT that contains lists. I want to add a list to RESULT only if it does not exist.

So

input = [1,2,3]
RESULT = [[5,6], [4,5,8]]

Now, RESULT.append(input) that gives

RESULT = [[5,6], [4,5,8], [1,2,3]]

Now if I try to append [5,6] it shouldn't be added as its already there.

I cannot use a set here so what's the alternative ?


Solution

  • def add(data_, value):
        if value not in data_:
            data_.append(value)
    
    data = [[5, 6], [4, 5, 8]]
    print(data)  # [[5, 6], [4, 5, 8]]
    add(data, [1, 2, 3])
    print(data)  # {(5, 6), (4, 5, 8), (1, 2, 3)}
    add(data, [5, 6])
    print(data)  # {(5, 6), (4, 5, 8), (1, 2, 3)}