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

python set
convert list to set python
python flatten list
python convert list of lists to a single list
python flatten list of strings
python list of lists
python convert list to set unhashable
convert list to string python

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.

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>])

Python : Convert list of lists or nested list to flat list – thispointer.com, In this article we will discuss different ways to convert a list of lists or Well we achieved our result, but there is an another way to do this,  The cmp () function is a built-in method in Python used to compare the elements of two lists. The function is also used to compare two elements and return a value based on the arguments passed. This value can be 1, 0 or -1. Note: cmp () build to function for python version 2, In python version 3 it is not available.

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))

Flatten a list of lists in one line in Python (Example), The old way would be to do this using a couple of loops one inside the other. While this works, it's clutter you can do without. This tip show how  You can use python set () function to convert list to set.It is simplest way to convert list to set. As Set does not allow duplicates, when you convert list to set, all duplicates will be removed in the set. Let’s understand with the help of example.

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}

Python, Using sorted() function will convert the set into list in a defined order. The only drawback of this method is that the elements of the set need to be sortable. Varun September 23, 2019 Python : Convert list of lists or nested list to flat list 2019-09-23T09:59:41+05:30 List, Python No Comment In this article we will discuss different ways to convert a list of lists or nested lists to a single flat list.

Python, The set function can be used to convert list into a set to remove repetition. print ( "The common elements from N lists : " + str (res)) to find common elements in three lists using sets · Python | Print all the common elements of two lists See your article appearing on the GeeksforGeeks main page and help other Geeks. Use another list ‘res’ and a for a loop. Using split () method of Python we extract each element from the list in the form of the list itself and append it to ‘res’.

Python Programming: Problem Solving, Packages and Libraries, In Python, sets cannot contain mutable elements, such as lists or dictionaries. But you can “convert” a list or a dictionary to a Python set using some operators. The update() method can take strings, list tuples or even other sets as its  Approach #2 : using sorted () method Using sorted () function will convert the set into list in a defined order. The only drawback of this method is that the elements of the set need to be sortable.

8.7. sets — Unordered collections of unique elements, Like other collections, sets support x in set , len(set) , and for x in set . As a result, sets cannot contain mutable elements such as lists or dictionaries. using the protocol described in section Protocol for automatic conversion to immutable. a __hash__() method, they can be used as set elements or as dictionary keys. Sets vs Lists and Tuples. Lists and tuples are standard Python data types that store values in a sequence. Sets are another standard Python data type that also store values. The major difference is that sets, unlike lists or tuples, cannot have multiple occurrences of the same element and store unordered values. Advantages of Python Sets

Comments
  • Check edits to my answer... let me know if they help...