Here are some commonly used dictionary methods in Python:
-
keys(): Returns a view object that displays a list of all the keys in the dictionary.my_dict.keys() -
values(): Returns a view object that displays a list of all the values in the dictionary.my_dict.values() -
items(): Returns a view object that displays a list of key-value pairs (tuples) in the dictionary.my_dict.items() -
update(): Updates the dictionary with elements from another dictionary or from an iterable of key-value pairs.my_dict.update({'age': 31, 'country': 'USA'}) -
pop(): Removes the specified key and returns its value. Raises aKeyErrorif the key is not found.age = my_dict.pop('age') -
popitem(): Removes and returns the last inserted key-value pair as a tuple. Raises aKeyErrorif the dictionary is empty.last_item = my_dict.popitem() -
clear(): Removes all items from the dictionary.my_dict.clear() -
copy(): Returns a shallow copy of the dictionary.new_dict = my_dict.copy()
These methods can help you manipulate and interact with dictionaries effectively. Let me know if you need more details on any specific method!
