Python Forum
Can't we add 1 and True to set ?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Can't we add 1 and True to set ?
#1
result=set()
result.add(1)
result.add(True)
result.add('Science')
print(result)
Result returned:
{1, 'P'}

I am adding the values 1 & True to the set and set does not consider True. I am assuming that this is happening because True=1 and set does not allow duplicates.

Is this behavior right?

In my case 1 is an int and True is a bool.
Reply
#2
Well, bool is a subclass of int. But the key point for sets is this: if hash(x) == hash(y) and x == y, then x and y are considered to be the same object for a set. This is the case for 1 and True (and also the case for 0 and False). It's also the case for 1 and 1.0.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Returning True or False vs. True or None trevorkavanaugh 6 9,231 Apr-04-2019, 08:42 AM
Last Post: DeaD_EyE

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020