Python Programming – Set

In this Page, We are Providing Python Programming – Set. Students can visit for more Detail and Explanation of Python Handwritten Notes Pdf.

Python Programming – Set

Set

A set is an unordered collection with no duplicate elements. Basic uses include membership testing, eliminating duplicate entries from sequence, mathematical operations like union, intersection, difference, symmetric difference etc. As mentioned in chapter 2, sets are of two types: set (mutable set) and frozenset (immutable set).

>>> a=set ( [ ’ spam ’ , ' eggs ' , 100 , 1234 ] ) 
>>> a
set ( [ ' eggs ' , 100 , 1234 , ' spam ' ] ) 
>>> a=set ( ' abracadabra ' )
>>> a
set ( [ ' a ' , ' r ' , ' b ' , ' c ' , ' d ' ] )
>>> a-frozenset ( [ ' spain ' , ' eggs ' , 100 , 1234 ] ) 
>>> a
frozenset ( [ ' eggs ' , 100 , 1234 , ' spam ' ] )

Being an unordered collection, sets do not record element position or order of insertion. Accordingly, sets do not support indexing, slicing, or other sequence-like behavior.

Set creation

A set can be created in many ways.

Using curly braces

Non-empty set (not frozenset) can be created by placing a comma-separated list of elements within braces. Curly braces cannot be used to create an empty set, because it will create an empty dictionary that will be discussed in the next section.

>>> { ' spam ' , ' eggs ' , 100 , 1234 } 
set ( [ 1234 , 100 , ' eggs ' , ' spam ' ] )

Set comprehension

Python also supports set comprehension:

>>> a= { x for x in ' abracadabra ' if x not in ' abc ' }
>>> a
set ( [ ' r ' , ' d ' ] )

Using built-in function

The built-in functions set ( ) and frozenset ( ) are used to create set and frozenset, respectively, whose elements are taken from iterable. If iterable is not specified, a new empty set is returned.

>>> a=set ( ( ' spam ' , ' eggs ' , 100 , 1234 ) )
>>> a
set ( [ ' eggs ' , 100 , 1234 , ' spam ' ] )
>>> set ( )
set ( [ ] ) 
>>> a=frozenset ( ( ' spam ' , ' eggs ' , 100 , 1234 ) )
>>> a
frozenset ( [ ' eggs ' , 100 , 1234 , ' spam ' ] )
>>> frozenset ( ) 
frozenset ( [ ] ) 
>>> set ( ' abc ' )==frozenset ( ' abc ' )
True

Deleting set

To explicitly remove an entire set, just use the del statement.

>>> ss=set ( ' abracadabra ' )
>>> ss
set ( [ ' a ' , ' r ' , ' b ' , ' c ' , ' d ' ] )
>>> del SS 
>>> SS
Traceback ( most recent call last ) :
File "<stdin>", line 1 , in <module>
NameError: name ' ss ' is not defined

Looping techniques

It is also possible to iterate over each element of a set. However, since set is unordered, it is not known which order the iteration will follow.

>>> ss=set ( ' abracadabra ' )
>>> ss
set ( [ ' a ' , ' r ' , ' b ' , ' c ' , ' d ' ] ) 
>>> for item in ss:
. . . print item
a
r
b
c
d

Membership operation

Set also support membership operation.

>>> a=set ( ' abracadabra ' )
>>> a
set ( [ ' a ' , " r ' r ' b ' , ' c ' , ' d ' ] )
>>> ' r ' in a 
True 
>>> ' rrr ' not in a 
True 
>>> a=frozenset ( ' abracadabra ' )
>>> a
frozenset ( [ ' a ' , ' r ' , ' b ' , ' c ' , ' d ' ] ) 
>>> ' r ' in a
True
>>> ' rrr ' not in a 
True