1. Introduction
Python dictionaries are an integral part of the language. They allow us to store and manage data in key-value pairs. To efficiently use dictionaries, Python provides a variety of methods that perform different operations on dictionaries, from adding and removing items to merging two dictionaries together.
Python Dictionary Methods:
- clear(): Removes all items from the dictionary.
- copy(): Returns a shallow copy of the dictionary.
- fromkeys(): Creates a new dictionary with keys from seq and values set to value.
- get(): Returns the value for a key if it exists in the dictionary.
- items(): Returns a view object that displays a list of dictionary's (key, value) tuple pairs.
- keys(): Returns a view object that displays a list of all the keys.
- pop(): Removes and returns an element from a dictionary with the provided key.
- popitem(): Removes the last inserted key-value pair.
- setdefault(): Returns the value of a key if the key is in the dictionary; if not, inserts the key with the specified value.
- update(): Updates the dictionary with elements from another dictionary object or from an iterable of key-value pairs.
- values(): Returns a view object that displays a list of all the values in the dictionary.
2. Program Steps
1. Create a dictionary for demonstration.
2. Perform and print the result of each method operation on the dictionary.
3. Code Program
# Create a dictionary
my_dict = {'a': 1, 'b': 2, 'c': 3}
# Use clear()
my_dict.clear()
print(f"After clear(): {my_dict}")
# Use copy()
my_dict = {'a': 1, 'b': 2, 'c': 3}
dict_copy = my_dict.copy()
print(f"After copy(): {dict_copy}")
# Use fromkeys()
keys = ['a', 'b', 'c']
default_value = 0
new_dict = dict.fromkeys(keys, default_value)
print(f"After fromkeys(): {new_dict}")
# Use get()
value_a = my_dict.get('a')
print(f"After get(): {value_a}")
# Use items()
dict_items = my_dict.items()
print(f"After items(): {dict_items}")
# Use keys()
dict_keys = my_dict.keys()
print(f"After keys(): {dict_keys}")
# Use pop()
value_popped = my_dict.pop('b')
print(f"After pop(): {value_popped}, {my_dict}")
# Use popitem()
item_popped = my_dict.popitem()
print(f"After popitem(): {item_popped}, {my_dict}")
# Use setdefault()
default = my_dict.setdefault('d', 4)
print(f"After setdefault(): {default}, {my_dict}")
# Use update()
second_dict = {'e': 5, 'f': 6}
my_dict.update(second_dict)
print(f"After update(): {my_dict}")
# Use values()
dict_values = my_dict.values()
print(f"After values(): {dict_values}")
Output:
After clear(): {} After copy(): {'a': 1, 'b': 2, 'c': 3} After fromkeys(): {'a': 0, 'b': 0, 'c': 0} After get(): 1 After items(): dict_items([('a', 1), ('b', 2), ('c', 3)]) After keys(): dict_keys(['a', 'b', 'c']) After pop(): 2, {'a': 1, 'c': 3} After popitem(): ('c', 3), {'a': 1} After setdefault(): 4, {'a': 1, 'd': 4} After update(): {'a': 1, 'd': 4, 'e': 5, 'f': 6} After values(): dict_values([1, 4, 5, 6])
Explanation:
Each method is called on the my_dict dictionary.
- clear() empties the dictionary.
- copy() creates a copy that is assigned to dict_copy.
- fromkeys() creates a new dictionary with specified keys and default value.
- get() retrieves the value for key 'a'.
- items(), keys(), and values() return view objects containing the dictionary's items, keys, and values, respectively.
- pop() removes the key 'b' and returns its value.
- popitem() removes and returns the last key-value pair.
- setdefault() inserts key 'd' with a default value of 4 since 'd' is not already in the dictionary.
- update() adds key-value pairs from second_dict to my_dict.
Each operation's result is printed to show the effect of the method on the dictionary.
Comments
Post a Comment
Leave Comment