1
liste0 = []
liste1 = []
s1 = input("kişileri gir")
s2 = input("kişileri gene gir")
s1 = s1.split(",")
s2 = s2.split(",")
liste0.append(s1)
liste1.append(s2)
print(set(liste0) ^ set(liste1))

#error is: TypeError: unhashable type: 'list'

Gladiones
  • 23
  • 4

1 Answers1

1

Do you mean to use extend instead of append?

liste0.extend(s1)
liste1.extend(s2)

When you use append, you have a nested list. For example, if you enter 1,2,3 at your first prompt, you get list0 = [['1', '2', '3']], because it appended the split input as a list as a single element to the empty list.

But you cannot have lists inside of sets, for example:

print(set([0, 1, 2]))
# {0, 1, 2}

print(set([0, [1, 2]]))
# TypeError: unhashable type: 'list'

From the set() docs:

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

From the hashable docs:

Most of Python’s immutable built-in objects are hashable; mutable containers (such as lists or dictionaries) are not; immutable containers (such as tuples and frozensets) are only hashable if their elements are hashable.

Timur Shtatland
  • 12,024
  • 2
  • 30
  • 47
  • You might want to explain why this possibly solves the problem. – mkrieger1 Apr 26 '21 at 14:59
  • I understood it well thank you very much. I have another small question. Is there any other way that is less complicated to compare 2 lists without using a set? – Gladiones Apr 26 '21 at 15:22
  • 1
    @Gladiones You can also do it with list comprehensions, but using sets is efficient and Pythonic. See also: https://stackoverflow.com/q/3462143/967621 – Timur Shtatland Apr 26 '21 at 15:44