0

What is the difference between this two expressions?

x = set(1) # TypeError: 'int' object is not iterable
x = {1}    # OK

Why does the first raise an error and the other not?

mkrieger1
  • 19,194
  • 5
  • 54
  • 65
buhtz
  • 10,774
  • 18
  • 76
  • 149
  • 2
    Both create sets. The constructor function expects to be passed an iterable (as the error indicates). See https://docs.python.org/3.8/library/stdtypes.html#set – Felix Kling Feb 18 '22 at 12:20
  • 2
    This might help too, first answer: https://stackoverflow.com/questions/53918325/differences-for-creating-set-using-set-or – Cow Feb 18 '22 at 12:22
  • 1
    Also, note same apply for other constructors like e.g. `list(1)` and `[1]`, and `tuple(1)` and `(1,)` – buran Feb 18 '22 at 12:29

0 Answers0