How to convert list of lists to a set in python so I can compare to other sets?

31,942

Solution 1

You would need to convert the inner lists to tuples, assuming each of those ObjectId('55119e14bf2e4e010d8b48f2') is hashable:

users_with_invites_ids_set = set(tuple(x) for x in users_with_invites_ids_list)

Working example:

>>> class ObjectId(object):
...   def __init__(self, v):
...     self.v = v
... 
>>> list_of_lists = [[ObjectId('55119e14bf2e4e010d8b48f2')], [ObjectId('54624128bf2e4e5e558b5a52')], [ObjectId('53a6e7bc763f4aa0308b4569')], [ObjectId('55241823bf2e4e59508b494c')]]
>>> set(tuple(x) for x in list_of_lists)
set([(<__main__.ObjectId object at 0x7f71483cfc50>,), (<__main__.ObjectId object at 0x7f71483cfd10>,), (<__main__.ObjectId object at 0x7f71483cfcd0>,), (<__main__.ObjectId object at 0x7f71483cfc90>,)])

In case if you are looking to create the set of the ObjectId's alone, you could do:

>>> set(x for lst in list_of_lists for x in lst)
set([<__main__.ObjectId object at 0x7f71483cfb10>, <__main__.ObjectId object at 0x7f71483db050>, <__main__.ObjectId object at 0x7f71483cfad0>, <__main__.ObjectId object at 0x7f71483cfd50>])

Solution 2

In case list of lists has lists of integer, you can use this function to convert it to set of one list :

outer_list = [] 
def lists_to_list(nested_lists): 
    for el in nested_lists: 
        if type(el) == list: 
            lists_to_list(el) 
        else: 
            outer_list.append(el)
    return set(outer_list)

Example:

nested_list = [[7, 7], [2, 3, 3, 0], [9, 1, 9, 1]]
print(lists_to_list(nested_list))

Output:

{0, 1, 2, 3, 7, 9}

Solution 3

While the accepted answer is good, if you'd like something simpler for comparison and don't want to deal with the immutability of tuples, you could also try it using good old-fashioned string comparison:

list1 = [[], [60], [95], [60, 95]]
list2 = [[], [95], [60], [60, 95]]
print(set(str(x) for x in list1) == set(str(x) for x in list2))
Share:
31,942
DBWeinstein
Author by

DBWeinstein

Co-Founder at HomeKeepr. Coding hobbyist. Always learning about everything.

Updated on January 18, 2020

Comments

  • DBWeinstein
    DBWeinstein over 4 years

    I have a list users_with_invites_ids_list, formed by loop where I append values to the list, in python that looks like this:

    ...[ObjectId('55119e14bf2e4e010d8b48f2')], [ObjectId('54624128bf2e4e5e558b5a52')], [ObjectId('53a6e7bc763f4aa0308b4569')], [ObjectId('55241823bf2e4e59508b494c')]]
    

    when I try:

        users_with_invites_ids_set = set(users_with_invites_ids_list)
    

    I get:

    TypeError: unhashable type: 'list'
    

    How do I convert this list of lists to a set?

    EDIT

    based on answer I've done the following:

    #convert list to set
    first_tuple_list = [tuple(lst) for lst in users_with_invites_ids_list]
    users_with_invites_ids_set = set(first_tuple_list)
    

    Which yields the following:

     (ObjectId('542ac5a6763f4a82188b4a51'),), (ObjectId('54496fe6bf2e4efe348bd344'),), (ObjectId('54c96339bf2e4ee62c8b48e0'),)])
    

    How do I get each ObjectId without the () around each one. It's keeping me from comparing this set to other set's of ids.