Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unable to convert list into set, raises "unhashable type: 'list' " error

So I'm trying to find all sub-lists of a list and here is what I have now. I'm new to Python and I don't understand why " Q3_ans=set(ans)" raises an error. I've tried to convert a list to set before and it works.

def f2(seq):
    '''
    This is the base case of the recursion from function all_sublists
    '''
    assert len(seq)==2
    assert isinstance(x,list)
    a,b=seq
    return [[a],[b],[a,b]]


def all_sublists(x):
    '''
    This function will generate all of the sublists of a list, not including the empty one, using recursion
    '''
    assert isinstance(x,list)
    ans=[]
    for i in range(0,len(x)-1):
        for j in range(1,len(x)):
            temp=[x[i],x[j]]
            temp=[f2(temp)]
            ans.extend(temp)
    Q3_ans=set(ans) 
    return Q3_ans

Here is the error when I run my code y=[1,2,3,4,5]

all_sublists(y)
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-108-f8b1bb0a7001> in <module>
----> 1 all_sublists(y)

<ipython-input-106-84f4f752e98e> in all_sublists(x)
     10             temp=[f2(temp)]
     11             ans.extend(temp)
---> 12     Q3_ans=set(ans)
     13     return Q3_ans

TypeError: unhashable type: 'list'
like image 473
家宁张 Avatar asked Oct 14 '18 17:10

家宁张


People also ask

How can we solve the error of Unhashable type list?

This error occurs when trying to hash a list, which is an unhashable object. For example, using a list as a key in a Python dictionary will cause this error since dictionaries only accept hashable data types as a key. The standard way to solve this issue is to cast a list to a tuple, which is a hashable data type.

How do you fix a TypeError Unhashable type dict?

The Python "TypeError: unhashable type: 'dict'" occurs when we use a dictionary as a key in another dictionary or as an element in a set . To solve the error, use a frozenset instead, or convert the dictionary into a JSON string before using it as a key.

What does Unhashable type list mean?

TypeError: unhashable type: 'list' usually means that you are trying to use a list as an hash argument. This means that when you try to hash an unhashable object it will result an error. For ex. when you use a list as a key in the dictionary , this cannot be done because lists can't be hashed.

How do I fix Unhashable type NumPy Ndarray?

Adding a NumPy Array to a Set Therefore, the code above should return an error: We can solve this by adding each array element instead of the array object into the set. This should add all the elements of the array to the set.


1 Answers

Here's the essence of the problem:

>>> set([[["a"],["b"],["a","b"]]])
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: unhashable type: 'list'

So, what does that mean?

set([iterable])

Return a new set ... object whose elements are taken from iterable. The elements of a set must be hashable.

hashable

An object is hashable if it has a hash value which never changes during its lifetime (it needs a __hash__() method), and can be compared to other objects (it needs an __eq__() method). Hashable objects which compare equal must have the same hash value.

Hashability makes an object usable as a dictionary key and a set member, because these data structures use the hash value internally.

All of Python’s immutable built-in objects are hashable; mutable containers (such as lists or dictionaries) are not.

The keywords here are mutable

Mutable objects can change their value but keep their id().

and immutable

An object with a fixed value. Immutable objects include numbers, strings and tuples. Such an object cannot be altered. A new object has to be created if a different value has to be stored. They play an important role in places where a constant hash value is needed, for example as a key in a dictionary.

So, you can't use a list as set element. A tuple would work:

>>> set([(("a"),("b"),("a","b"))])
{('a', 'b', ('a', 'b'))}
like image 70
handle Avatar answered Sep 18 '22 05:09

handle