A set is an unordered sequence of non-repeating elements that can be created using curly braces { } or the set() function.
It is a very important and frequently used concept in Python. Whether it is in the daily development process or in the interview process, it will often be encountered. Today, I will come to 11 "unknown" collection usages.
Programmer's Treasure Store : https://github.com/Jackpopc/CS-Books-Store
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 but not 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 containing the elements of set_1 and the elements of set_2, 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) : Take the intersection of two sets and return only the elements that exist in both 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) : Check if all elements of set_1 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) : Check if all elements of set_2 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) : Check 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() : removes a random element from a collection.
discard(value) : removes the specified element from a collection, without raising an error if the element does not exist.
remove(value) : removes the specified element from a collection, or raises an error if the element does not exist.
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() : removes all elements from the collection.
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'}