11 Python Set Methods

Hello, I'm Aya Bouchiha,
on this beautiful day, we'll discuss 11 python set methods.

difference(set)

set_1.difference(set_2): this method helps you to get the difference between two sets, in other words, it lets you get the elements that exist in set_1 and don't exist in the given set (set_2).

# example 1
recepie_requirements = {'orange', 'chocolate', 'salt', 'pepper'}
what_I_have = {'apple', 'banana','salt'}
# I have to buy orange chocolate pepper
print('I have to buy', *recepie_requirements.difference(what_I_have))

# example2
all_subscribers = {"aya", "john", "smith", "sparf", "kyle"}
admins = {"aya", "sparf"}
users = all_subscribers.difference(admins)
# {'kyle', 'smith', 'john'}
print(users)

union(set)

set_1.union(set_2): (set_1 U set_2) this set method returns a set which contains the set_1's elements and also the set_2's elements, In addition, the returned set contains only unique elements.

admins = {'aya', 'sparf'}
users = {'aya','kyle', 'smith', 'john'}

all_subscribers = admins.union(users)

# {'smith', 'aya', 'sparf', 'kyle', 'john'}
print(all_subscribers)

intersection(set)

**set_1.intersection(set_2): returns only the elements that exist in set_1 and set_2.

shop = {'orange', 'pepper', 'banana', 'sugar'}
what_I_have = {'orange', 'sugar'}

# I should not buy {'orange', 'sugar'} because I have them!
print(f'I should not buy {shop.intersection(what_I_have)} because I have them!')

issubset()

set_1.issubset(set_2): checks if all set_1's elements exist in set_2.

nearest_library_books = {"the power of now", 'why we sleep', 'rich dad poor dad'}
necessary_books = {'atomic habits','the 48 laws of power', 'why we sleep'}

if necessary_books.issubset(nearest_library_books):
  print('yes, you can buy these books from your nearest library')
else:
  print('unfortunately, you have to go to another library')

# unfortunately, you have to go to another library

issuperset()

set_1.issuperset(set_2): checks if all set_2's elements exist in set_1.

nearest_library_books = {"the power of now", 'why we sleep', 'rich dad poor dad'}
necessary_books = {'atomic habits','the 48 laws of power', 'why we sleep'}

if nearest_library_books.issuperset(necessary_books):
  print('yes, you can buy these books from your nearest library')
else:
  print('unfortunately, you have to go to another library')

# unfortunately, you have to go to another library

isdisjoint(set)

isdisjoint(set): checks if the two sets do not contain common elements.

set_1 = {12, 38, 36}
set_2 = {4, 40, 12}
# means can set_1 element - set_2 element == 0 ?
can_substruction_be_zero = set_1.isdisjoint(set_2)
print(can_substruction_be_zero) # False

discard(value), remove(value), pop()

pop(): deletes a random element from a set.
discard(value): deletes a specified element in a set without raising an error if It does not exist.
remove(value): deletes a specified element in a set, and raises an error if It is not exists

users = {"Aya Bouchiha", "John Doe", "Kyle Smith", "Nabo Snay"}
deleted_account = 'Aya Bouchiha'

users.discard(deleted_account)
users.discard('Hi!')
print(users) # {'Kyle Smith', 'John Doe', 'Nabo Snay'}

users.remove('Kyle Smith') 
print(users) # {'Nabo Snay', 'John Doe'}

users.pop()
print(users) # {'John Doe'}

users.remove('Hello!') # KeyError

clear()

clear(): helps you to delete all set methods.

countries = {'Morocco', 'UK', 'Spain', 'USA', 'UK'}

print(len(countries)) # 4

countries.clear()

print(countries) # set()
print(len(countries)) # 0

copy

copy(): this method lets you get a copy of the specified set of elements

countries = {'Morocco', 'UK', 'Spain', 'USA', 'UK'}

print(countries) # {'UK', 'Morocco', 'Spain', 'USA'}
print(countries.copy()) # {'UK', 'Morocco', 'Spain', 'USA'}

Summary

  • set_1.difference(set_2): returns the difference between two sets.
  • set_1.union(set_2): (set_1 U set_2) returns a set which contains the set_1's elements and also the set_2's elements, In addition, the returned set contains only unique elements.
  • set_1.intersection(set_2): returns only the elements that exist in set_1 and set_2.
  • set_1.issuperset(set_2): checks if all set_2's elements exist in set_1.
  • set_1.issubset(set_2): checks if all set_1's elements exist in set_2.
  • isdisjoint(set): checks if the two sets does not contains common elements.
  • pop(): deletes a random element from a set.
  • discard(value): deletes a specified element in a set without rasing an error if It is not exists.
  • remove(value): deletes a specified element in a set, and rases an error if It is not exists
  • clear(): deletes all set methods.
  • copy(): returns a copy of the specified set elements

Reference

Suggested Posts

To Contact Me:

Happy codding!

21