Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In what version of Python was set initialisation syntax added

Tags:

python

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

  • Sets in Python 2.4 Docs
  • What's new in Python 3.0
like image 676
Casebash Avatar asked Oct 12 '25 07:10

Casebash


1 Answers

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

like image 57
Ian Clelland Avatar answered Oct 14 '25 21:10

Ian Clelland