Python Datatype - Part 4 (Sets)
Abdulmutalib Idris
Head of IT and Media / Full Stack Developer @ Agro Preciso LTD
Python Sets
As we learnt earlier Set is one of 4 built-in data types in Python used to store collections of data, the other 3 are List, Tuple, and Dictionary.
myset = {"Toyota", "Mercedes", "Ford", "Honda", "IVM"}
print(myset)
Note: Sets are unordered, so you cannot be sure in which order the items will appear when printed.
myset = {"Toyota", "Mercedes", "Ford", "Honda", "IVM", "Ford"}
print(myset)
myset = {"Toyota", True, "Mercedes", 0, 1, 2, False, "Ford", "Honda", "IVM", "Ford"}
print(myset)
Get the Length of a Set
len() function is use to determine the number of items in a set
myset = {"Toyota", "Mercedes", "Ford", "Honda", "IVM"}
print(len(myset))
Set Items - Data Types
Items store in a set can be of any data type. it can even be a combination of multiple data type
set1 = {"apple", "banana", "cherry"}
set2 = {1, 5, 7, 9, 3}
set3 = {True, False, False}
set4 = {"abc", 34, True, 40, "male"}
Access Set Items
Set item can not be accessed by index or key but looping through the set, items can be getting and you can also check if a value in present in the set by using the "in" keyword
myset = {"Toyota", "Mercedes", "Ford", "Honda", "IVM"}
for x in myset:
print(x)
myset = {"Toyota", "Mercedes", "Ford", "Honda", "IVM"}
print("Ford" in myset)
Change Items
Set item can only be created, they can not be change by any means, but you can add new items to the set.
Add Set Items
The add() method is use to add item to a set
myset = {"Toyota", "Mercedes", "Ford", "Honda", "IVM"}
print(myset)
myset.add("Tesla")
print(myset)
Add a Set to a Set
update() method is use to add items from another set to a set
myset = {"Toyota", "Mercedes", "Ford", "Honda", "IVM"}
myset2 = {"Tesla", "Nissan"}
myset.update(myset2)
print(myset)
update() can be use on any other iterable object to a set, like lists, tuples, dictionaries
领英推荐
myset = {"Toyota", "Mercedes", "Ford", "Honda", "IVM"}
mylist = ["Volkswagen", "Tesla", "Nissan"]
myset.update(mylist)
print(myset)
Remove Item
remove() or discard() method can both be used to remove item from a set
myset = {"Toyota", "Mercedes", "Ford", "Honda", "IVM"}
myset.remove("Toyota")
print(myset)
Note: If the item to remove does not exist, remove() will raise an error.
myset = {"Toyota", "Mercedes", "Ford", "Honda", "IVM"}
myset.discard("Toyota")
print(myset)
Note: If the item to remove does not exist, discard() will NOT raise an error.
pop() can also be use to remove item from a set, but it will remove any random item, no way to be sure which item will get removed
myset = {"Toyota", "Mercedes", "Ford", "Honda", "IVM"}
x = myset.pop()
print(x)
print(myset)
clear() method use to empty the set
del keyword use to delete the set completely from memory
Join Sets
joining of sets can be done in several ways:
- Using union() method: returns a new set containing all items from both sets
myset = {"Toyota", "Mercedes", "Ford", "Honda", "IVM"}
myset2 = {"Tesla", "Nissan"}
myset3 = myset.union(myset2)
print(myset3)
- Using update() method: inserts all the items from one set into another
myset = {"Toyota", "Mercedes", "Ford", "Honda", "IVM"}
myset2 = {"Tesla", "Nissan"}
myset.update(myset2)
print(myset)
Note: Both union() and update() will exclude any duplicate items.
- Using intersection_update() method: method will keep only the items that are present in both sets (keep only duplicate)
myset = {"Toyota", "Mercedes", "Ford", "Honda", "IVM"}
myset2 = {"Tesla", "Nissan", "Ford", "Honda"}
myset.intersection_update(myset2)
print(myset)
- Using intersection() method: return a new set, that only contains the items that are present in both sets.
myset = {"Toyota", "Mercedes", "Ford", "Honda", "IVM"}
myset2 = {"Tesla", "Nissan", "Ford", "Honda"}
myset3 = myset.intersection(myset2)
print(myset3)
- Using symmetric_difference_update() method: keep only the elements that are NOT present in both sets
myset = {"Toyota", "Mercedes", "Ford", "Honda", "IVM"}
myset2 = {"Tesla", "Nissan", "Ford", "Honda"}
myset.symmetric_difference_update(myset2)
print(myset)
- Using symmetric_difference() method: return a new set, that contains only the elements that are NOT present in both sets
myset = {"Toyota", "Mercedes", "Ford", "Honda", "IVM"}
myset2 = {"Tesla", "Nissan", "Ford", "Honda"}
myset3 = myset.symmetric_difference(myset2)
print(myset3)
Software Engineer
1 年Please tell me about Python programming learning