You are here

Set in Python

SETS in PYTHON

A set is an unordered collection of items. Every element of a set is unique, i.e. there are no duplicate. A set itself is a mutable i.e. we can add or remove items in a set. Items of a set are immutable i.e. we cannot modify items of a set using its index. A set is declared using {} or in-built set() function

How many items in a set:

A set can have any number of integer, float, tuple, string types etc

s={3,5,”Hello”, 3.14}

What cannot be in a set:

A set cannot have mutable elements like lists, sets or dictionaries as its elements.

So it will be wrong to create a set:

>>>s={3,5, [6,7], 3.14}

error – set cannot have list as the set element

Set elements cannot be accessed using indexing, so they are also immutable on elements:

>>> a={4,5,’c’,”uuuu”, 4.555}

>>> a

{‘c’, 4.555, 4, 5, ‘uuuu’}

>>> a[0]           #TypeError: ‘set’ object is not subscriptable

Declaration of a set:

A set can be declared using following techniques:

i. Using comma seperated items inside the curly braces as:

>>>my_set = {3,2,5, 7,8}

>>>another_set = {3,2, “Hello”, 3.14, }

ii. Using the set() function:

A Set can be created using the set() function. Set() function is used t convert a list, tuplte into a set. The set() function is mutable so we can add list, another set tuple to a set function. As an example:

my_set = set() #create an empty set

>>> a=[3,4,5,6,7]

>>> b=set(a) #converts a list into a set

>>> b

{3, 4, 5, 6, 7}

>>> a=set([2,3,4]) # convert a list into a set

>>> a

{2, 3, 4}

>>> a=set(“tstsss”) # Converts a string into a set of unique elements

>>> a

{‘t’, ‘s’}

Adding and Updating set in python

>>> b=set()

>>> b.add(2)

>>> b

{2}

>>> b.add(5)

>>> b

{2, 5}

>>>a11={2,3}

>>> a11.add(4)

>>> a11

{2, 3, 4}

>>> a11.update([7,6], {‘a’,’g’})

>>> a11

{2, 3, 4, 6, 7, ‘a’, ‘g’}

Removing elements from a set in python

>>> s2=set(“Ravinder”)

>>> s2

{‘r’, ‘a’, ‘i’, ‘e’, ‘v’, ‘R’, ‘n’, ‘d’}

>>> print(s2)

{‘r’, ‘a’, ‘i’, ‘e’, ‘v’, ‘R’, ‘n’, ‘d’}

>>> s2.pop()

‘r’

>>> s2

{‘a’, ‘i’, ‘e’, ‘v’, ‘R’, ‘n’, ‘d’}

>>> s2.dicard(‘v’)

>>>s2

{‘a’, ‘i’, ‘e’, ‘R’, ‘n’, ‘d’}

>>> s2.dicard(‘u’) #no change on s2, also no error on trying to discard non existing item

>>>s2

{‘a’, ‘i’, ‘e’, ‘R’, ‘n’, ‘d’}

>>> s={2,3,4,5,6}

>>> s

{2, 3, 4, 5, 6}

>>> s.remove(6)

>>> s

{2, 3, 4, 5}

>>> s.remove(5)

>>> s

{2, 3, 4}

>>> s.discard() #TypeError: discard() takes exactly one argument (0 given)

>>> s={2, 3, 4, 5, 6}

>>> s.discard(7) #no error, on discarding non-existing item

>>> s

{2, 3, 4, 5, 6}

>>> s.remove(7) #KeyError: 7, error on removing non-existing item

Mathemetical Operations on Sets

Create two sets a and b

>>> a

{3, 4, 5, 6}

>>> b

{8, 5, 6, 7}

>>> print(a)

{3, 4, 5, 6}

Union of sets

>>> a.union(b)

{3, 4, 5, 6, 7, 8}

>>> a|b

{3, 4, 5, 6, 7, 8}

>>> b.union(a)

{3, 4, 5, 6, 7, 8}

Intersection of sets

>>> a&b

{5, 6}

>>> a.intersection(b)

{5, 6}

>>> b.intersection(a)

{5, 6}

Difference of sets

>>> a.difference(b)

{3, 4}

>>> a-b

{3, 4}

symmetric difference of sets

>>> a.symmetric_difference(b)

{3, 4, 7, 8}

>>> a^b

{3, 4, 7, 8}

Leave a Reply

Top
error: Content is protected !!