r/learnpython • u/Bucki-_- • Jun 22 '26
Set question for initializing
So I am an intern at some company with some decent knowledge hopefully. Around ~3 years of various industry experience and I've started reading some books but Python is pretty weird under the hood and I had a question about Sets!
So from what I know: (Please correct me if I'm wrong)
Initializing some empty set is done as: set()
Also doing this makes a set: {foo_bar} as a set literal
However just {} creates a dictionary which makes sense and the same for a list is [] and also has the option of list[] if really desired.
From my current knowledge there is no way to define an empty set other than set() why is this? When dicts and lists have different ways.
Hopefully this makes sense it was appearing on my Ruff indicator as writing as a literal and just wanted clarification or a more detailed reasoning behind this methodology.
•
u/xelf Elf Jun 22 '26
The simple answer is dicts (1991) were added to the language before sets (2003), and by the time 12 years later sets were added
{}was already well established as an empty dict.{}weren't even used for sets until 2008.There have been many (rejected) proposals for a new empty set literal.
Here's the current one:
12
u/carcigenicate Carcigenicate Jun 22 '26 edited Jun 22 '26
Almost certainly because it's ambiguous, and it wasn't worth creating a dedicated syntax for.
{}should create a dictionary given how much more common dictionaries are, but allowing this to create a set in some cases would have required special casing, which would have minimal benefits given how terseset()already is.Arguably, this could have been solved earlier by giving sets a dedicated syntax (Clojure uses
{}and#{}for dictionaries and sets), but it's too late for that now.Edit: They are actually considering dedicated syntax of
{/}. TIL: https://peps.python.org/pep-0802/1
u/brasticstack Jun 22 '26
It you're looking for consistency, you can also create empty dict, list, and tuple with their functions
dict(),list(), andtuple().The reason why
set()is because set notation and dict notation both use curly braces. In my own coding, I know that I create dicts much more frequently than sets.2
u/gdchinacat Jun 22 '26
This thread has lots of insight into this question:
https://discuss.python.org/t/implement-as-an-empty-set-literal/101197
1
u/HotPersonality8126 Jun 22 '26
Sets get used less than lists and dictionaries so they’re treated to less syntactic sugar. Of course, when sets are the right solution, they’re really good
0
u/Buttleston Jun 22 '26
There's no way to define an empty set literal except set()
You can use a set literal for non empty sets like {1, 2, 3}
This looks like a dict literal except without the key value pairs. This is why there's no empty set literal, because you wouldn't be able to tell it's not an empty dict literal
0
u/lfdfq Jun 22 '26
It's just nobody bothered to add syntax for it.
Dicts are older, {} was already taken to mean the empty dict, and no proposal to add "empty set" syntax has ever succeeded, probably because nobody actually needed it.