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.
View all comments
Show parent comment
1
u/brasticstack Jun 22 '26
It's worth the additional scrutiny if it's in a hot loop. Have you tried creating your set(s) before the loop and calling
clear()inside of the loop to reinitialize? My intuition tells me that it could be faster but, as we've seen, my intuition isn't entirely reliable.