T={}
Is T a set or a dictionary?
type(T) returns dict but isn't it an ambiguous notation? OR am my missing something? sys.getsizeof(T) gives 148 and I think this is just an anticipation of dict to be on the safer side.
Please share your thoughts.
T={}
Is T a set or a dictionary?
type(T) returns dict but isn't it an ambiguous notation? OR am my missing something? sys.getsizeof(T) gives 148 and I think this is just an anticipation of dict to be on the safer side.
Please share your thoughts.
It's a dict. Period.
If you want an empty set, use set()
.
If you want a filled set, you can use either the {1, 2, 3, 4}
(since Python 3) notation or the set((1, 2, 3, 4))
notation.
d = {} # A dictionary
d = dict() # also a dictionary
s = set() # A set
This is also stated in the docs:
Curly braces or the set() function can be used to create sets. Note: to create an empty set you have to use set(), not {}; the latter creates an empty dictionary, a data structure that we discuss in the next section.
Especially the examples afterwards make it clear. (To be honest, I'm a bit confused by the "Curly braces [...] can be used to create sets.")
Speculation: The {}
notation is shorter and dictionarys are more often used. Also, it is a common notation in other languages, too.
One way to think about dictionaries is as a set of (key, value) tuples. Hence the set-like notation does make sense.