Set.discard(), remove() & pop() Python
.remove(x) This operation removes element x from the set. If element x does not exist, it raises a KeyError. The .remove(x) operation returns None. Example: >>> s = set([1, 2, 3, 4, 5, 6, 7, 8, 9]) >>> s.remove(5) >>> print s set([1, 2, 3, 4, 6, 7, 8, 9]) >>> print s.remove(4) None >>> print s set([1, 2, 3, 6, 7, 8, 9]) >>> s.remove(0) KeyError: 0 .discard(x) This operation also removes element x from the set. If element x does not exist, it does not raise a KeyError. T
View Solution →Set .intersection() Operation
.intersection() The .intersection() operator returns the intersection of a set and the set of elements in an iterable. Sometimes, the & operator is used in place of the .intersection() operator, but it only operates on the set of elements in set. The set is immutable to the .intersection() operation (or & operation). >>> s = set("Hacker") >>> print s.intersection("Rank") set(['a', 'k']) >>> print s.intersection(set(['R', 'a', 'n', 'k'])) set(['a', 'k']) >>> print s.intersection(['
View Solution →Set .union() Operation
.union() The .union() operator returns the union of a set and the set of elements in an iterable. Sometimes, the | operator is used in place of .union() operator, but it operates only on the set of elements in set. Set is immutable to the .union() operation (or | operation). Example >>> s = set("Hacker") >>> print s.union("Rank") set(['a', 'R', 'c', 'r', 'e', 'H', 'k', 'n']) >>> print s.union(set(['R', 'a', 'n', 'k'])) set(['a', 'R', 'c', 'r', 'e', 'H', 'k', 'n']) >>> print s
View Solution →Set.difference() Operation
.difference() The tool .difference() returns a set with all the elements from the set that are not in an iterable. Sometimes the - operator is used in place of the .difference() tool, but it only operates on the set of elements in set. Set is immutable to the .difference() operation (or the - operation). >>> s = set("Hacker") >>> print s.difference("Rank") set(['c', 'r', 'e', 'H']) >>> print s.difference(set(['R', 'a', 'n', 'k'])) set(['c', 'r', 'e', 'H']) >>> print s.difference([
View Solution →Set.symmetric_difference() Operation python
.symmetric_difference() The .symmetric_difference() operator returns a set with all the elements that are in the set and the iterable but not both. Sometimes, a ^ operator is used in place of the .symmetric_difference() tool, but it only operates on the set of elements in set. The set is immutable to the .symmetric_difference() operation (or ^ operation). >>> s = set("Hacker") >>> print s.symmetric_difference("Rank") set(['c', 'e', 'H', 'n', 'R', 'r']) >>> print s.symmetric_difference
View Solution →