2
a = [5,5,6,7,7,7]
b = set(a)
def test(lst):
    if lst in b:
        return 1
    else:
        return 0
print(test(a))  #this gives me error list unhashable  
print(test,a)   #how this is working
for i in  filter(test, a):
    print(i,end=" ")

output : 5 5 6 7 7 7

why print(test(a)) giving me error?

11
  • please disclose the output's Commented Jan 23, 2020 at 3:58
  • What is the purpose of the function test? b is a set of numbers, so it cannot contain lst which is a list, not a number. Commented Jan 23, 2020 at 3:59
  • what is your requirement/ expected output ? Commented Jan 23, 2020 at 4:00
  • I am not understanding how it is working. Commented Jan 23, 2020 at 4:01
  • I want to understand how function is calling and working Commented Jan 23, 2020 at 4:01

3 Answers 3

4

So let me tell you why print(test, a) is working first.

When you call print(func) without function call (or in other words, without doing print(func()), what you are actually doing is printing the functions object, which happens to return a str representation of the function that contains the location of the function in memory.

def func():
    return "Hello World!"

print(func)      # >> <function func at 0x100858950>
print(func())    # >> Hello World!
print(func, "a") # >> <function func at 0x100858950> a

Now in regard to why you are receiving your error TypeError: unhashable type: 'list', this is due to the nature of a set and of a list. Notice the following will reproduce your exact error:

foo = [1, 2, 3]
set = {4, 5}

print(foo in set)

Outputs:

Traceback (most recent call last):
  File "test.py", line 8, in <module>
    print(foo in set)
TypeError: unhashable type: 'list'

This error is due to how set() works in Python. Notice the documentation for set() (here) states:

Return a new set or frozenset object whose elements are taken from iterable. The elements of a set must be hashable. To represent sets of sets, the inner sets must be frozenset objects. If iterable is not specified, a new empty set is returned.

A "hashable" element, defined by Python's doc here states:

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.

What is going on is that a Python's list object is not hashable due to its very nature -- no __hash__() functionality.

class Foo:
    foo = 1

a = 1
b = Foo()
c = "Hello World!"
d = [1, 2, 3]

Outputs:

1
280623165
-5794655848578721369
Traceback (most recent call last):
  File "test.py", line 14, in <module>
    print(d.__hash__())
TypeError: 'NoneType' object is not callable

In other words, lists are not hashable types in Python, and therefore you cannot check if they exist within a set since set will utilize the __hash__() functionality of objects to check if they exist within the set.

Sign up to request clarification or add additional context in comments.

1 Comment

I can understand now.
0

print(test,a) just printing function of test memory adress like <function test at 0x10d02d1e0> and list of a. Its the same thing like print("abcd",212) , So in this print , your test function doesnt run.

Your function is running in

print(test(a))

And you have some error here

1 Comment

It's not "printing the address", it's printing the string representation of the function object, which is the default object representation inherited from object, which happens to include a memory address
-1

In your code, you are checking whether list a is in the set b or not, here list a is a mutable object, you can't do like this. That's why it's throwing the error TypeError: unhashable type: 'list'. Read more about in operator

And print(test, a) is simply printing the test function object and the value of a

Try the code below,

a = [5,5,6,7,7,7]
b = set(a)
for i in filter(lambda x: x in b, a):
    print(i, end=" ")

Output

5 5 6 7 7 7 

2 Comments

@paraspatel Updated, hope you can understand it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.