Search code examples
pythonlistsetunique

How to compare two list of sets effectively in python?


I have a list of sets:

list = [{1,2}, {2,3}, {1,3}, {2,1} etc]

I need a list where every element appears once. creating a set of the list fails with a set is not hashable error If I create another list, and add every element once with if not in list then append list works, but now I have to deal with a list with aroun 600 000 value pairs, and it takes forever. Is there a more efficent way?


Solution

  • Referencing your comment; as you mentioned this worked, I'll post my comment as a proper answer.

    set(map(tuple, list_))
    

    Output:

    {(1, 2), (1, 3), (2, 3)}
    

    Please note:
    I've changed the your list variable named to list_, as it was overwriting the built-in.

    This method appears to scale linearly. Time over a list of 1000 sets was 206 us and 10000 was 2.19 ms.