In the previous blogs, we learned how to create, access, and modify dictionaries and sets in Python. In this blog, we will dive deeper into these data structures and explore the various methods available to us.
Dictionary Methods
keys(): Returns a view object of the keys in the dictionary.
my_dict = {"apple": 2, "banana": 3, "orange": 4}
print(my_dict.keys()) # Output: dict_keys(['apple', 'banana', 'orange'])
values(): Returns a view object of the values in the dictionary.
my_dict = {"apple": 2, "banana": 3, "orange": 4}
print(my_dict.values()) # Output: dict_values([2, 3, 4])
items(): Returns a view object of the key-value pairs in the dictionary.
my_dict = {"apple": 2, "banana": 3, "orange": 4}
print(my_dict.items()) # Output: dict_items([('apple', 2), ('banana', 3), ('orange', 4)])
get(): Returns the value associated with a key. If the key is not found, it returns the default value specified as a second argument (or None if no default value is specified).
my_dict = {"apple": 2, "banana": 3, "orange": 4}
print(my_dict.get("apple")) # Output: 2
print(my_dict.get("pear", 0)) # Output: 0
pop(): Removes and returns the value associated with a key. If the key is not found, it raises a KeyError (or returns the default value specified as a second argument if provided).
my_dict = {"apple": 2, "banana": 3, "orange": 4}
print(my_dict.pop("banana")) # Output: 3
print(my_dict) # Output: {"apple": 2, "orange": 4}
Set Methods
add(): Adds an element to the set.
my_set = {1, 2, 3, 4, 5}
my_set.add(6)
print(my_set) # Output: {1, 2, 3, 4, 5, 6}
update(): Adds multiple elements to the set.
my_set = {1, 2, 3, 4, 5}
my_set.update([6, 7, 8])
print(my_set) # Output: {1, 2, 3, 4, 5, 6, 7, 8}
remove(): Removes an element from the set. Raises a KeyError if the element is not found.
my_set = {1, 2, 3, 4, 5}
my_set.remove(3)
print(my_set) # Output: {1, 2, 4, 5}
discard(): Removes an element from the set. Does not raise an exception if the element is not found.
my_set = {1, 2, 3, 4, 5}
my_set.discard(6)
print(my_set) # Output: {1, 2, 3, 4, 5}
intersection(): Returns a new set containing only the elements that are common to both sets.
my_set1 = {1, 2, 3, 4, 5}
my_set2 = {4, 5, 6, 7, 8}
print(my_set1.intersection(my_set2)) # Output: {4, 5}
union(): Returns a new set containing all the elements from both sets.
my_set1 = {1, 2, 3, 4, 5}
my_set2 = {4, 5, 6, 7, 8}
print(my_set1.union(my_set2)) # Output: {1, 2, 3, 4, 5, 6, 7, 8}
difference(): Returns a new set containing the elements that are in the first set but not in the second set.
my_set1 = {1, 2, 3, 4, 5}
my_set2 = {4, 5, 6, 7, 8}
print(my_set1.difference(my_set2)) # Output: {1, 2, 3}
symmetric_difference(): Returns a new set containing the elements that are in either of the sets but not in both.
my_set1 = {1, 2, 3, 4, 5}
my_set2 = {4, 5, 6, 7, 8}
print(my_set1.symmetric_difference(my_set2)) # Output: {1, 2, 3, 6, 7, 8}
intersection_update(): Updates the set to contain only the elements that are common to both sets.
my_set1 = {1, 2, 3, 4, 5}
my_set2 = {4, 5, 6, 7, 8}
my_set1.intersection_update(my_set2)
print(my_set1) # Output: {4, 5}
difference_update(): Updates the set to contain only the elements that are in the first set but not in the second set.
my_set1 = {1, 2, 3, 4, 5}
my_set2 = {4, 5, 6, 7, 8}
my_set1.difference_update(my_set2)
print(my_set1) # Output: {1, 2, 3}
symmetric_difference_update(): Updates the set to contain only the elements that are in either of the sets but not in both.
my_set1 = {1, 2, 3, 4, 5}
my_set2 = {4, 5, 6, 7, 8}
my_set1.symmetric_difference_update(my_set2)
print(my_set1) # Output: {1, 2, 3, 6, 7, 8}
Conclusion
Dictionaries and sets are incredibly powerful data structures in Python, and the methods discussed in this blog allow us to perform various operations efficiently. It is essential to understand these methods to take full advantage of these data structures in your Python programs.
0 Comments