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
  • 14,486
  • 4
  • 43
  • 54
buhtz
  • 8,057
  • 11
  • 59
  • 115
  • 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 – user56700 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