2
s = ["this", "that", "this"]

Why does set(s) work but {s} fails with

TypeError: unhashable type: 'list'
Coddy
  • 549
  • 4
  • 18

1 Answers1

8

It's because they mean different things. set(s) iterates s to create a set, whereas the literal syntax {s} just attempts to create a set containing the single element s.

>>> set("abc")
{'a', 'b', 'c'}
>>> {"abc"}
{'abc'}

Try {*s} instead for the equivalent of set(s).

wim
  • 338,267
  • 99
  • 616
  • 750