I only just noticed this feature today!
s={1,2,3} #Set initialisation
t={x for x in s if x!=3} #Set comprehension
t=={1,2}
What version is it in? I also noticed that it has set comprehension. Was this added in the same version?
Resources
The sets
module was added in Python 2.3, but the built-in set type was added to the language in 2.4, with essentially the same interface. (As of 2.6, the sets
module has been deprecated.)
So you can use sets as far back as 2.3, as long as you
import sets
But you will get a DeprecationWarning
if you try that import in 2.6
Set comprehensions, and the set literal syntax -- that is, being able to say
a = { 1, 2, 3 }
are new in Python 3.0. To be very specific, both set literals and set comprehensions were present in Python 3.0a1, the first public release of Python 3.0, from 2007. Python 3 release notes
The comprehensions and literals were later implemented in 2.7. 3.x Python features incorporated into 2.7
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With