0% found this document useful (0 votes)
3 views

Sets

Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Sets

Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 37

Sets

By Kaloyan Totev
Defining a set – set(<iter>)
• x = set(['foo', 'bar', 'baz', 'foo', 'qux'])
• {'qux', 'foo', 'bar', 'baz'}

x = set(('foo', 'bar', 'baz', 'foo', 'qux'))


• {'qux', 'foo', 'bar', 'baz'}
Defining a set – set(<Obj>, <Obj>,
…)
• x = {'foo', 'bar', 'baz', 'foo', 'qux'}
• {'qux', 'foo', 'bar', 'baz'}

• >>> x = {'q', 'u', 'u', 'x'}


• {'x', 'q', 'u'}
What is the difference ?
• >>> {'foo'}

• >>> set('foo')
What is the difference ?
• >>> {'foo'}
• {'foo'}

• >>> set('foo')
• {'o', 'f'}
There is only one way to define
empty set
• >>> x = set()
• >>> type(x)
• <class 'set'>

• >>> x = {}
• >>> type(x)
• <class 'dict'>
The elements in a set can be objects
of different types:
• >>> x = {42, 'foo', 3.14159, None}
• {None, 'foo', 42, 3.14159}
Elements must be immutable
• x = {42, 'foo', (1, 2, 3), 3.14159}
{42, 'foo', 3.14159, (1, 2, 3)}

• >>> a = [1, 2, 3]
• >>> {a}
• Traceback (most recent call last):
• File "<pyshell#70>", line 1, in <module>
• {a}
• TypeError: unhashable type: 'list'
Length and membership
• >>> x = {'foo', 'bar', 'baz'}

• >>> len(x)
•3

• >>> 'bar' in x
• True
Set operations
Union Operation
• >>> x1 = {'foo', 'bar', 'baz'}
• >>> x2 = {'baz', 'qux', 'quux'}
• >>> x1 | x2
• {'baz', 'quux', 'qux', 'bar', 'foo’}

• BOTH OPERANDS MUST BE SETS


Union method
• >>> x1.union(x2)
• {'baz', 'quux', 'qux', 'bar', 'foo’}

• X1 MUST BE A SET, x2 MUST BE ITERABLE


Union of more sets
• >>> a = {1, 2, 3, 4} • >>> a.union(b, c, d)
• >>> b = {2, 3, 4, 5} • {1, 2, 3, 4, 5, 6, 7}
• >>> c = {3, 4, 5, 6}
• >>> d = {4, 5, 6, 7} • >>> a | b | c | d
• {1, 2, 3, 4, 5, 6, 7}
intersection
>>> x1 = {'foo', 'bar', 'baz’}
>>> x2 = {'baz', 'qux', 'quux’}
>>> x1.intersection(x2)
{'baz’}
>>> x1 & x2
{'baz'}
Intersection of multiple sets
• >>> a = {1, 2, 3, 4} • >>> a.intersection(b, c,
• >>> b = {2, 3, 4, 5} d)

• >>> c = {3, 4, 5, 6} • {4}

• >>> d = {4, 5, 6, 7} • >>> a & b & c & d


• {4}
Union difference
Union difference
• >>> x1 = {'foo', 'bar', 'baz'}
• >>> x2 = {'baz', 'qux', 'quux'}
• >>> x1.difference(x2)
• {'foo', 'bar'}
• >>> x1 - x2
• {'foo', 'bar'}
• >>> a = {1, 2, 3, 30, • >>> a.difference(b, c)
300} • {1, 2, 3}
• >>> b = {10, 20, 30, 40}
• >>> c = {100, 200, 300, • >>> a - b - c
400}
• {1, 2, 3}
Symmetric difference
Symmetric difference
• >>> x1 = {'foo', 'bar', 'baz'}
• >>> x2 = {'baz', 'qux', 'quux'}
• >>> x1.symmetric_difference(x2)
• {'foo', 'qux', 'quux', 'bar'}
• >>> x1 ^ x2
• {'foo', 'qux', 'quux', 'bar'}
• >>> a = {1, 2, 3, 4, 5} • >>> a ^ b ^ c
• >>> b = {10, 2, 3, 4, 50} • {100, 5, 10}
• >>> c = {1, 50, 100}
Isdisjoint()
• >>> x1 = {'foo', 'bar', • >>> x2 - {'baz'}
'baz'} • {'quux', 'qux'}
• >>> x2 = {'baz', 'qux', • >>> x1.isdisjoint(x2 -
'quux'} {'baz'})
• >>> x1.isdisjoint(x2) • True
• False
Issubset()
• >>> x1 = {'foo', 'bar', 'baz'}
• >>> x1.issubset({'foo', 'bar', 'baz', 'qux', 'quux'})
• True

• >>> x2 = {'baz', 'qux', 'quux'}


• >>> x1 <= x2
• False
Issubset()
• >>> x = {1, 2, 3, 4, 5}
• >>> x.issubset(x)
• True
• >>> x <= x
• True
Modifying a set
• >>> x1 = {'foo', 'bar', • >>> x1.update(['corge', 'garply'])
'baz'} • >>> x1
• >>> x2 = {'foo', 'baz', • {'qux', 'corge', 'garply', 'foo', 'bar', 'baz’}
'qux'}
• >>> x1 |= x2 • X1.add(‘me’)
• >>> x1 • {'qux’,’me’, 'corge', 'garply', 'foo', 'bar',

• {'qux', 'foo', 'bar', 'baz'} 'baz’}


Remove an element
• >>> x = {'foo', 'bar', • >>> x.remove('qux')
'baz'} • Traceback (most recent call
last):
• File "<pyshell#58>", line 1,
• >>> x.remove('baz')
in <module>
• >>> x
• x.remove('qux')
• {'bar', 'foo'} • KeyError: 'qux'
Remove an element
• Discard()

• Pop()

• Clear()
Frozen sets
• >>> x = frozenset(['foo', • Python provides another
'bar', 'baz']) built-in type called a
frozenset, which is in all
• >>> x
respects exactly like a set,
• frozenset({'foo', 'baz', except that a frozenset is
'bar'}) immutable. You can perform
non-modifying operations on
a frozenset
Iterating through a set
• for element in my_set:
• print(element)
Membership
• if 3 in my_set:
print("3 is in the set")
• Sets are commonly used in Python for tasks that involve
handling collections of unique
• elements, such as removing duplicates from a list or
performing set-based mathematical
• Operations
exercises
• Write a Python program to remove an item from a set if it is
present in the set.
• Write a Python program to check if two given sets have no
elements in common.
• Write a Python program to remove all duplicates from a given list
of strings and return a list of unique strings. Use the Python set
data type.
exercises
• Write a Python program to find the third largest number from a
given list of numbers.Use the Python set data type.
• Given two sets of numbers, write a Python program to find the
missing numbers in the second set as compared to the first and
vice versa. Use the Python set.

You might also like