0

i can't add set to python

you can add tuple to python set

a={1,2,3,4,5}
a.add((10,11,12))

when i try same with set

a={1,2,3,4,5}
a.add({10,11,12})
TypeError: unhashable type: 'set'
MisterMiyagi
  • 44,374
  • 10
  • 104
  • 119

2 Answers2

0

You can add frozensets to a set, since they are hashable (because they are immutable).

a = {1,2,3,4,5}
a.add(frozenset([10,11,12]))
chepner
  • 497,756
  • 71
  • 530
  • 681
-1

I'd assume you are trying to get:

{1, 2, 3, 4, 5, 10, 11, 12}

You are appending a set which is unhashable. And when you add the tuple you will get:

{1, 2, 3, 4, 5, (10, 11, 12)}

You are trying you get the union:

a.union({10,11,12})

Or:

a | {10,11,12}
Jab
  • 26,853
  • 21
  • 75
  • 114