Python 2 tries to provide a sort order for almost everything; dictionaries are no exception.
Dictionaries are arbitrarily but consistently ordered when compared to one another to ensure that you can sort a heterogenous list that contains them. You should not derive any meaning from their comparisons, really.
Python 3 abandoned the notion that all objects should be ordered relative to one another and using comparison operators other than equality and identity on dictionaries raises a TypeError
instead.
Sets overload comparison operators to signal subsets. If set_a
is a subset of set_b
then set_a < set_b
is true. See the set types documentation.
To translate all this to your specific examples:
- Your two sets are not subsets of one another; one has the value
4
and the other the value1
which are not shared. Testing for a subset fails in both directions. {1:2,2:3,3:4} < {2:5,3:6,4:7,5:8}
is True because the first dictionary has fewer keys. The choice is arbitrary, there isn’t any specific meaning to this other than that this means the two dictionaries will always be ordered consistently within one Python version.- Your last sample compares two different types. A tuple is never equal to a list, even if they have the same contents.
solved Conditional operator in python dictonaries and set