- Day : Monday
- Date : 2025-09-15
- Time : 10:23
- Tags : #python #Sets #Revised #
- References : [[RevisedNotes]] , [[FunctionsSets]] , [[ImportantQuestionsSets1]]
- Branch of : Python > RevisedNotes > RevisedNotesSets
- Author : dx
- it will give the length of a set
- TypeErrror if non iterable is passed
- if we assigned a set with duplicates the it will consider it as a single and then return length of a non duplicate vslues
- as set is a unordered dstatype so sorted function on a set returns a list of sorted elements of set
- TypeError if not comararble
- Removes duplicates automatically (since set already has unique elements).
- Sorting works only if elements are comparable.
- sorting by the length of a str
s = {"apple", "kiwi", "banana"}
print(sorted(s, key=len)) # ['kiwi', 'apple', 'banana']- Sorting large sets is O(n log n) → can be slow for very large datasets.
These work as same as for others
- adds a single element to the set
- returns none as modifies the original set
- TypeError is not hashable like list,dict
- cant be chaned as it returns none
- no duplicates allowed if we add a existing element nothing happens but it will be shown as non duplicated
- cant add multiple values at once it will give TypeError if more than one arg
- order is not graunteed
- Returns None
- TypeError if not a iterable or
- add all elements fom the itertable like a extend function but uunordered
- we can add values from a list , dict but list must only have hashable values and only keys will be added from dict as they are hashable
- range(1,10) can also be used as it returns a iterator obj
- multiple iterable values can be added
- it is like a bulk union but it updates in the set
- update the str charachters
s = {"a"}
s.update("hello")
print(s) # {'a', 'h', 'e', 'l', 'o'}- it will remove the element from the set
- returns None modifies the original
- KeyError if not present
- TypeError if unhashable passed
#to safley remove element by remove()
x=6
if x in a:
a.remove(x)
print(a)- it will remove the element from the set
- it will returns None modifies the original
- Doesnt raise KeyError if not present use it more over remove it gives None
- TypeErro if unhashable as a arg
- Safe removal → no error if element missing.
- Works only with hashable elements.
- Safe removal (avoid KeyError)
s = {1, 2, 3}
s.discard(4) # safe, no crash
print(s) # {1, 2, 3}- it will remove any random value from the set
- return the element removed
- TypeError if any arg passed
- KeyError id set is empty
- safe use
return set.pop() if set else "EMPTY"- emptiess the set
- but doesnot delete it from memory
- returns None
- No Errors
- TypeError if arg is passed
- After
.clear(), the set is still valid but empty - Works safely even if the set is already empty.
s1 = {1, 2}
s2 = s1
s1.clear()
print(s2) # set() (both cleared because they point to same object)Don’t confuse with del set_obj
.clear()→ empties the set but keeps the variable alive.del s→ deletes the variable itsel
- creates a shallow copy of set
- returns a new copy of set
- No errors
- TypeError if any arg is passed
- Creates a new set object with the same elements as the original.
- Changes to the copied set do not affect the original set (and vice versa).
- Since sets can only hold immutable objects, shallow copy is sufficient (no deep copy needed).
- Attribute Errors ony work with for which it is defined
lst = [1, 2, 3]
lst.copy() # ✅ works for lists too
num = 5
num.copy() # ❌ AttributeError- Confusion with
=(assignment)
s1 = {1, 2, 3}
s2 = s1 # ❌ points to same set (not a copy)
s2 = s1.copy() # ✅ creates independent copy- Returns a new set containing all unique elements from all iterables
- TypeError if non iterable
- Returns a new set
a = {1}
b = {2}
c = {3}
print(a.union(b, c)) # {1, 2, 3}- originals are unchanged doesnt modifies the original
a = {1, 2}
b = {3, 4}
a.union(b)
print(a) # {1, 2} (unchanged)- Works with any iterable, not just sets
- But input eements must be hashable.
set.union(a, b, c)- Here
setis the class itself, not an instance. set.union()expects at least one argument to be a set (or any iterable).- Syntax:
set.union(set1, set2, ...)→set1is the first set, then additional iterables. a = {1, 2} b = {2, 3} c = {3, 4} print(set.union(a, b, c)) # {1, 2, 3, 4}
- Here
a.union(b, c)- This is calling
.union()on a set instancea. - Works the same way: combines
awith all other iterables.print(a.union(b, c)) # {1, 2, 3, 4}
- This is calling
Difference:
set.union()→ class method call, first argument must be a set (or iterable).a.union()→ instance method, cleaner and more common in practice.
- Yes, dicts are iterables, but iterating over a dict yields keys only.
- Accepts one or more iterables (set, list, tuple, string, dict, etc.).
- Returns a new set containing elements that are common to all iterable
- TypeError if non iterable
- returna a new set
- Only elements present in all iterables are included.
- Works with any iterable, but input elements must be hashable.
- Can chain multiple iterables:
a = {1, 2}
b = {3, 4}
a.union(b)
print(a) # {1, 2} (unchanged)- Intersection with non-set iterables
a = {1, 2, 3}
b = [2, 3, 4]
print(a.intersection(b)) # {2, 3}- same story of set.intersection(...) as above using set as class
- returns the difference
- returns a new set
- the set of elements which are in set but not in set1,set2 ......
- TypeError if not iterable
- returns a new set original unchanged
-
sethere is the class, not a set instance -
Syntax:
set.difference(set1, set2, set3, ...) -
set1must be a set instance (or any iterable) -
Returns a new set containing elements in
set1but not in the others
Example:
a = {1, 2, 3, 4}
b = {3, 4, 5}
c = {4, 5, 6}
print(set.difference(a, b, c)) # {1, 2}Works the same way as a.difference(b, c) — the first argument is treated as the set to subtract from.
set.difference()is a class method that expects the first argument to be a set instance (or at least a set-like object).- If the first argument is not a set, Python will raise a
TypeError
- modifies the original of first set when used with set as class
- returns none
- non iterable or unhashavle values make a TypError
- it will update to set1 or first set when used as a classs
- returns None
- TypeError if non iterable or unhashable vlueds
same as for above
- returns a new set
- give the element that is in either in 1st or 2nd if present in 2nd then it will not add it
- take only one arg
- TypeError if non iterabe or element are unhashable and if more that one arg
- Important: Unlike
unionorintersection, you can only pass ONE iterable. Trying to pass multiple iterables like(set1, set2, list)will raise an error. - Original remains unchanged
- Accepts one iterable (set, list, tuple, string, dict, etc.)
- output is bool
- check if disjoint or not
- TypeError if non iterable or elements unhashable
- Accepts one iterable (set, list, tuple, string, dict, etc.)
- Returns a boolean:
True→ no common elementsFalse→ at least one element is common
- Check if two sets/lists have no overlap
a = {1,2,3} b = [4,5,6] print(a.isdisjoint(b)) # True - Useful in algorithms
- Example: verify if a group of elements is completely independent from another group.
- Quick condition checks in filtering or validation.
- returns True or False
- TypeError if non iterable or unhashable element
- Checks whether all elements of
set1are present in the iterable. - Accepts one iterable (set, list, tuple, string, dict, etc.)
- return true or false
- TypeError if non iterable or unhashable element
- Accepts one iterable (set, list, tuple, string, dict, etc.)
- Checks whether all elements of the iterable are present in
set1. :⚠️ This is the opposite of.issubset().