Set methods<\/strong><\/p>\nBelow are the methods of both set and frozenset objects. Note that the non-operator versions of these methods accept any iterable as an argument, while their operator-based counterparts require their arguments to be set (set and frozenset).<\/p>\n
isdisjoint ( other ) \nReturn True, if the set has no elements in common with other. Sets are disjoint, if and only if their intersection is the empty set.<\/p>\n
>>> s1=set ( [ 5 , 10 , 15 , 20] )\r\n>>> s2=set ( [ 30 , 35 , 40 ] )\r\n>>> s1 . isdisjoint ( s2 )\r\nTrue \r\n>>> s1=frozenset ( [ 5 , 10 , 15 , 20 ] )\r\n>>> s2=frozenset ( [ 30 , 35 , 40 ] )\r\n>>> s1 . isdisjoint ( s2 )\r\nTrue\r\n>>> s1=set ( [ 5 , 10 , 15 , 20 ] )\r\n>>> s2=frozenset ( [30 , 35 , 40 ] ) \r\n>>> s1 . isdisjoint ( s2 )\r\nTrue<\/pre>\nissubset ( other ) \nTest whether every element in the set is in other.<\/p>\n
>>> s1=set ( [ 5 , 15 ] )\r\n>>> s2=set ( [ 5 , 10 , 15 , 20 ] )\r\n>>> s1 . issubset ( s2 )\r\nTrue\r\n>>> s1 . issubset ( ( 5 , 10 , 15 , 20 ) )\r\nTrue\r\n>>> s1=frozenset ( [ 5 , 15 ] )\r\n>>> s2=frozenset ( [ 5 , 10 , 15 , 20 ] )\r\n>>> s1 . issubset ( s2 )\r\nTrue\r\n>>> s1 . issubset ( ( 5 , 10 , 15 , 20 ) )\r\nTrue<\/pre>\nThe operator based version of the the above method is set<=other.<\/p>\n
>>> s1=set ( [ 5 , 15 ] )\r\n>>> s2=frozenset ( [ 5 , 10 , 15 , 20 ] )\r\n>>> s1<=s2\r\nTrue<\/pre>\nThe operator based version setcother test whether the set is a proper subset of other, that is, set<=other and set!=other.<\/p>\n
>>> s1=set ( [ 5 , 15 ] )\r\n>>> s2=frozenset ( [ 5 , 10 , 15 , 20 ] )\r\n>>> s1<s2 \r\nTrue<\/pre>\nissuperset (other).<\/p>\n
Test whether every element in other is in the set.<\/p>\n
>>> s1=set ( [ 5 , 15 ] )\r\n>>> s2=set ( [ 5 , 10 , 15 , 20 ] ) \r\n>>> s2 . issuperset ( s1 )\r\nTrue\r\n>>> s2 . issuperset ( ( 5 , 15 ) )\r\nTrue\r\n>>> s1=frozenset ( [ 5 , 15 ] )\r\n>>> s2=frozenset ( [ 5 , 10 , 15 , 20 ] )\r\n>>> s2 . issuperset ( s1 )\r\nTrue\r\n>>> s2 . issuperset ( ( 5 , 15 ) )<\/pre>\nThe operator based version of the the above method is set >=other.<\/p>\n
>>> s1=set ( [ 5 , 15 ] )\r\n>>> s2=frozenset ( [ 5 , 10 , 15 , 20 ] )\r\n>>> s1>=s2 \r\nFalse<\/pre>\nThe operator-based version set> another test whether the set is a proper superset of other, that is, set>=other and set! =other.<\/p>\n
>>> s1=set ( [ 5 , 15 ] )\r\n>>> s2=frozenset ( [ 5 , 10 , 15 , 20 ] )\r\n>>> s1<s2 \r\nFalse<\/pre>\nunion ( other, . . . )<\/p>\n
Return a new set with elements from the set and all others.<\/p>\n
>>> s1=set ( [ 5 , 15 ] )\r\n>>> s2= [ 15 , 20 , 25 ]\r\n>>> s3=frozenset ( [ 30 , 35 , 40 ] )\r\n>>> s1 . union ( s2 , s3 )\r\nset ( [ 35 , 20 , 5 , 40 , 25 , 30 , 15 ] )<\/pre>\nThe operator based version of the above method is set | other. . .<\/p>\n
>>> s1=set ( [ 5 , 15 ] )\r\n>>> s2=set ( [ 15 , 20 , 25 ] ) \r\n>>> s3=frozenset ( [ 30 , 35 , 40 ] )\r\n>>> s1 | s2 | s3\r\nset ( [ 35 , 20 , 5 , 40 , 25 , 30 , 15 ] )<\/pre>\nintersection ( other , . . .)<\/p>\n
Return a new set with elements common to the set and all others .<\/p>\n
>>> s1=set ( [ 5 , 10 , 15 ] ) \r\n>>> s2= [ 15 , 20 , 25 , 10 ]\r\n>>> s3=frozenset ( [ 30 , 15 , 35 , 40 , 10 ] ) \r\n>>> s4= ( 40 , 50 , 10 , 15 , 20 )\r\n>>> si . intersection ( s2 , s3 , s4 ) \r\nset ( [ 10 , 15 ] )<\/pre>\nThe operator based version of the above method is set&other . . .<\/p>\n
>>> s1=set ( [ 5 , 10 , 15] )\r\n>>> s2=set ( [ 15 , 20 , 25 , 10 ] )\r\n>>> s3=frozenset ( [ 30 , 15 , 35 , 40 , 10 ] )\r\n>>> s4=frozenset ( [ 40 , 50 , 10 , 15 , 20 ] )\r\n>>> s1&s2&s3&s4 \r\nset ( [ 10 , 15 ] )<\/pre>\ndifference ( other, . . . )<\/p>\n
Return a new set with elements in the set that are not in the others.<\/p>\n
>>> s1=set ( [ 5 , 10 , 15 ] )\r\n>>> s2= [ 15 , 20 , 25 , 10 ]\r\n>>> s3=frozenset ( [ 30 , 15 , 35 , 40 , 10 ] )\r\n>>> s4= ( 40 , 50 , 10 , 15 , 20 )\r\n>>> s3.difference ( s1 , s2 , s4 ) \r\nfrozenset ( [ 35 , 30 ] )<\/pre>\nThe operator based version of the above method is set-other- . . .<\/p>\n
>>> s1-set ( [ 5 , 10 , 15 ] )\r\n>>> s2=set ( [ 15 , 20 , 25 , 10 ] )\r\n>>> s3=frozenset ( [ 30 , 15 , 35 , 40 , 10 ] )\r\n>>> s4=frozenset ( [ 40 , 50 , 10 , 15 , 20 ] )\r\n>>> s1 - s2 - s3 - s4 \r\nset ( [ 5 ] )\r\n>>> s3 - s1 - s2 - s4 \r\nfrozenpet ( [ 35 , 30 ] )<\/pre>\nsymmetric_difference ( other )<\/p>\n
Return a new set with elements in either the set or other but not both.<\/p>\n
>>> s1=set ( [ 5 , 10 , 15 ] )\r\n>>> s2= [ 15 , 20 , 25 , 10 ]\r\n>>> s1 . symmetric_difference ( s2 ) \r\nset ( [ 25 , 20 , 5 ] )<\/pre>\nThe operator based version of the the above method is set Aother.<\/p>\n
>>> s1=set ( [ 5 , 10 , 15 ] )\r\n>>> s2=frozenset ( [ 15 , 20 , 25 , 10 ] )\r\n>>> s1\u2227 s2\r\nset ( [ 25 , 20 , 5 ] ) \r\n>>> s2\u2227s1\r\nfrozenset ( [ 25 , 20 , 5 ] )<\/pre>\ncopy ( ) \nReturn a copy of the set.<\/p>\n
>>> s=set ( [ 5 , 10 , 15 , 20 ] )\r\n>>> a=s.copy ( )\r\n>>> a\r\nset ( [ 10 , 20 , 5 , 15 ] )\r\n>>> s=frozenset ( [ 5 , 10 , 15 , 20] )\r\n>>> a=s.copy ( )\r\n>>> a\r\nfrozenset ( [ 10 , 20 , 5 , 15 ] )<\/pre>\nThe following methods are available for set and do not apply to immutable instances of frozenset.<\/p>\n
update ( other , . . . )<\/p>\n
Update the set, adding elements from all others.<\/p>\n
>>> s1=set ( [ 5 , 15 ] )\r\n>>> s2= ( 15 , 20 , 25 )\r\n>>> s3=frozenset ( [ 30 , 35 , 40] )\r\n>>> s1 . update ( s2 , s3 )\r\n>>> s1\r\nset ( [ 35 , 20 , 5 , 40 , 25 , 30 , 15 ] )<\/pre>\nThe operator based version of the above method is set | =other | . . .<\/p>\n
>>> s1=set ( [ 5 , 15 ] )\r\n>>> s2=set ( [ 15 , 20 , 25 ] )\r\n>>> s3=frozenset ( [ 30 , 35 , 40 ] )\r\n>>> s1|= s2 | s3 \r\n>>> s1\r\nset ( ' [ 35 , 5 , 40 , 15 , 20 , 25 , 30 ] )<\/pre>\nintersection_update ( other , . . .)<\/p>\n
Update the set, keeping only elements found in it and all others.<\/p>\n
>>> s1=set ( [ 5 , 10 , 15 ] )\r\n>>> s2= [ 15 , 20 , 25 , 10 ] \r\n>>> s3=set ( [ 30 , 15 , 35 , 40 , 10 ] ) \r\n>>> s4= ( 40 , 50 , 10 , 15 , 20 )\r\n>>> s1 . intersection_update ( s2 , s3 , s4 )\r\n>>> s1\r\nset ( [ 10 , 15 ] )<\/pre>\nThe operator based version of the the above method is set&=other& . . .<\/p>\n
>>> s1=set ( [ 5 , 10 , 15] )\r\n>>> s2=set ( [ 15 , 20 , 25 , 10] )\r\n>>> s3=frozenset ( [ 30 , 15 , 35 , 40 , 10 ] )\r\n>>> s4=frozenset ( [ 40 , 50 , 10 , 15 , 20 ] )\r\n>>> s1&=s2&s3&s4 \r\n>>> s1\r\nset ( [10 , 15 ] )<\/pre>\ndifference_update(other, . . .)<\/p>\n
Update the set, removing elements found in others.<\/p>\n
>>> s1=frozenset ( [ 5 , 10 , 15 ] )\r\n>>> s2= [ 15 , 20 , 25 , 10 ]\r\n>>> s3=set ( [ 30 , 15 , 35 , 40 , 10 ] )\r\n>>> s4= ( 40 , 50 , 10 , 15 , 20 )\r\n>>> s3 . difference_update ( s1 , s2 , s4 )\r\n>>> s3\r\nset ( [ 35 , 30 ] )<\/pre>\nThe operator based version of the above method is set-=other | . . .<\/p>\n
>>> s1=frozenset ( [ 5 , 10 , 15 ] )\r\n>>> s2=frozenset ( [ 15 , 20 , 25 , 10 ] )\r\n>>> s3=set ( [ 30 , 15 , 35 , 40 , 10 ] )\r\n>>> s4=frozenset ( [ 40 , 50 , 10 , 15 , 20 ] )\r\n>>> s3-=s1 | s2 | s4 \r\n>>> s3\r\nset ( [ 35 , 30 ] )<\/pre>\nsymmetric_difference_update(other) \nUpdate the set, keeping only elements found in either set, but not in both.<\/p>\n
>>> s1=set ( [ 5 , 10 , 15 ] ) \r\n>>> s2= [ 15 , 20 , 25 , 10 ]\r\n>>> s1 . symmetric_dif ference_update ( s2 )\r\n>>> s1\r\nset ( [ 25 , 20 , 5 ] )<\/pre>\nThe operator based version of the the above method is set\/’=other.<\/p>\n
>>> s1=set ( [ 5 , 10 , 15 ] ) \r\n>>> s2=frozenset ( [ 15 , 20 , 25 , 10 ] )\r\n>>> s1 A=s2 \r\n>>> s1\r\nset ( [ 25 , 20 , 5 ] )<\/pre>\nadd ( elem )<\/p>\n
The method adds element elem to the set.<\/p>\n
>>> s=set ( [ 5 , 10 , 15 , 20 ] )\r\n>>> s.add ( 25 )\r\n>>> s\r\nset ( [ 25 , 10 , 20 , 5 , 15 ] )<\/pre>\nremove ( elem )<\/p>\n
Remove element elem from the set. Raises KeyError, if elem is not contained in the set.<\/p>\n
>>> s=set ( [ 5 , 10 , 15 , 20 ] )\r\n>>> s.remove ( 15 )\r\n>>> s\r\nset ( [ 10 , 20 , 5 ] )\r\n>>> s=set ( [ 5 , 10 , 15 , 20 ] )\r\n>>> s . remove ( 100 )\r\nTraceback ( most recent call last ) :\r\nFile \" <stdin> \" , line 1 , in <module>\r\nKeyError: 100<\/pre>\ndiscard ( elem )<\/p>\n
Remove element elem from the set if it is present. It is difference from remove () in a way that it does not raise KeyError if elem is not present in the set.<\/p>\n
>>> s=set ( [ 5 , 10 , 15 , 20 ] )\r\n>>> s . discard ( 15 )\r\n>>> s\r\nset ( [ 10 , 20 , 5 ] )\r\n>>> s . discard ( 100 )\r\n>>> s\r\nset ( [ 10 , 20 , 5 ] )<\/pre>\npop ( )<\/p>\n
Remove and return an arbitrary element from the set. Raises KeyError, if the set is empty.<\/p>\n
>>> s=set ( [ 5 , 10 , 15 , 20] ) \r\n>>> s . pop ( )\r\n10\r\n>>> s\r\nset ( [ 20 , 5 , 15 ] )\r\n>>> s . pop ( )\r\n20\r\n>>> s\r\nset ( [ 5 , 15 ] )<\/pre>\nclear ( )<\/p>\n
Remove all elements from the set.<\/p>\n
>>> s=set ( [ 5 , 10 , 15 , 20 ] )\r\n>>> s . clear ( )\r\n>>> s \r\nset ( [ ] )<\/pre>\n","protected":false},"excerpt":{"rendered":"In this Page, We are Providing Python Programming \u2013 Set Methods. Students can visit for more Detail and Explanation of Python Handwritten Notes\u00a0Pdf. Python Programming \u2013 Set Methods Set methods Below are the methods of both set and frozenset objects. Note that the non-operator versions of these methods accept any iterable as an argument, while …<\/p>\n
Python Programming \u2013 Set Methods<\/span> Read More »<\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"spay_email":"","jetpack_publicize_message":"","jetpack_is_tweetstorm":false,"jetpack_publicize_feature_enabled":true},"categories":[5],"tags":[],"yoast_head":"\nPython Programming \u2013 Set Methods - Python Programs<\/title>\n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n\t \n\t \n\t \n