Set is a collection of distinct objects. Objects in a set can be of any data type. Set in Python also a collection data type such as list or tuple.
set() is one of the built-in functions. It takes any sequence object (list, tuple or string) as argument and returns a set object
ex:
L1 = ["Rohan", "Physics", 21, 69.75]
s1 = set(L1)
T1 = (1, 2, 3, 4, 5)
s2 = set(T1)
string = "TutorialsPoint"
s3 = set(string)
print (s1)
print (s2)
print (s3)
The set() function returns a set object from the sequence, discarding the repeated elements in it. Set is a collection of distinct objects. Even if you repeat an object in the collection, only one copy is retained in it.
Since set is not a sequence data type, its items cannot be accessed individually as they do not have a positional index (as in list or tuple). Set items do not have a key either (as in dictionary) to access. You can only traverse the set items using a for loop.
add()
method in set class adds a new element. If the element is already present in the set, there is no change in the set.update()
method of set class includes the items of the set given as argument. If elements in the other set has one or more items that are already existing, they will not be included.union()
method of set class also combines the unique items from two sets, but it returns a new set object.remove()
method removes the given item from the set collection, if it is present in it. However, if it is not present, it raises KeyError.discard()
method in set class is similar to remove() method. The only difference is, it doesn’t raise error even if the object to be removed is not already present in the set collection.pop()
method in set class removes an arbitrary item from the set collection. The removed item is returned by the method. Popping from an empty set results in KeyError.clear()
method in set class removes all the items in a set object, leaving an empty set.difference_update()
method in set class updates the set by removing items that are common between itself and another set given as argument.difference()
method is similar to difference_update()
method, except that it returns a new set object that contains the difference of the two existing sets.intersection_update()
method, the set object retains only those items which are common in itself and other set object given as argument.intersection()
method in set class is similar to its intersection_update() method, except that it returns a new set object that consists of items common to existing sets.symmetric_difference_update()
method updates a set with symmetric difference between itself and the set given as argument.symmetric_difference()
method in set class is similar to symmetric_difference_update() method, except that it returns a new set object that holds all the items from two sets minus the common items.copy()
method in set class creates a shallow copy of a set object.myset = set()
myset.add(1)
print(myset)
myset.add(2)
print(myset)
myset.add(1)
print(myset)
#It will mot add 1 again as sets will consider only uniquw value
mylist = [1,1,1,3,3,3,2,2,2,7,7,7,4,4,4]
mylist = set(mylist)
print(mylist)
#unorder collections of unique reference
s1={1,2,3,4,5}
s2={4,5,6,7,8}
s3 = s1|s2 # Joining sets
print (s3)
Result:
{1}
{1, 2}
{1, 2}
{1, 2, 3, 4, 7}
{1, 2, 3, 4, 5, 6, 7, 8}
Main Page | Next Chapter: Python - Arrays |