Welcome

sampleqa.in tutorials, python tutorial

Python tutorial


Python Set Methods

methoddescription
clearremoves all elements
copydoes shallow-copy
addAdds an element into the list.if element already exists do nothing
removeremoves element,otherwise keyError exction
popRemoves arbitrary set element
updateset1.update(set2), All unique elements from both sets are updated in set1,set2 is unchanged. This is equal to add 2 sets
unionall elements from both sets
intersectionelements exists in both sets
differenceset1-set2, i.e elements from set1, which are not in set2
difference_updateRemove all elements from another set from this set
symetric_differenceReturn set with uncommon elements from both sets
symetric_difference_updatesame as Symmetric-difference, but removes LHS set elements, and updates with uncommon elements from both sets. RHS set unchanged
intersection_updateSame as intersetion, In intersection method returns a new set, but in this case updates set1(LHS) with intersected values, Set2(RHS) unchanged
issubsetreturns True, when set1 elements contained in set2
issupersetreturns True when set2 all elements are contained in set2
discardremoves element from list

Set Remove and discard methods Difference

    Both methods are used to remove a element from the Set object.
discard method removes element if found, otherwise do nothing
remove methood also removes element if found, otherwise KeyError exception thrown

ADS