Python Dictionary Methods
Python Dictionary Methods
Python has a set of built-in methods that dictionary objects can call. All the python dictionary methods are as follow:
Methods | Description |
clear() | The dictionary.clear() method removes all the elements from the dictionary. |
copy() | The dictionary.copy() method returns a copy of the specified Dictionary. |
fromkeys() | The dictionary.fromkeys() method returns a dictionary with the specified keys and values. |
get() | This method returns the value for the given key. |
items() | The dictionary.items() method returns a list containing a tuple for each key value pair |
keys() | This method returns View Object of All Keys |
pop() | The dictionary.pop() method removes and returns element having the specified key. |
popitem() | This method removes the last inserted key-value pair |
setdefault() | The dictionary.setdefault() method inserts Key with a Value if Key is not Present and returns the value of the specified key |
update() | The dictionary.update()updates the dictionary with the specified key-value pairs |
values() | This method returns a list of all the values in the dictionary |
Example 1
# Python program explaining # the dictionary methods # initialising the dictionary fruits = { "banana" : "apple", "orange": "mango", "grapes": 5 } # printing the Dictionary print("Dictionary:",fruits) # the setdefault() method # returning the value of the key. print("The setdefault() method..") defaultValue=fruits.setdefault("grapes","vitamin c") print("\nValue of the key: \n", defaultValue) # the popitem() method # pop the last element fruits.popitem() print("\nAfter poping the last item..") print('Remaining dictionary: ', fruits) # the pop() method # removing the "banana" element from the dictionary. leftFruit=fruits.pop("banana") print("\nMy final lists of fruits are\n", fruits)
Output
Dictionary: {'orange': 'mango', 'banana': 'apple', 'grapes': 5} The setdefault() method.. Value of the key: 5 After poping the last item.. Remaining dictionary: {'banana': 'apple', 'grapes': 5} My final lists of fruits are {'grapes': 5}
Example 2
# Python program explaining # the dictionary methods # initializing the student dictionary student = {'studentName': 'Reema','Roll No':'15cs1029'} print("The actual Student Details:",student) # initializing the second dicitonary with updated values updatedStudent= {'studentName': 'Reema','Roll No':'15ME1028','age':22} # the dictionary.update() method # update the value of key 'Roll' student.update(updatedStudent) print("Student details after updation:") print(student) # the dictionary.get() method # will return the key value of name and language print('Student Name: ', student.get('studentName')) print('Roll No: ', student.get('Roll No')) # the dictionary.items() method # returns a view object that displays a list of a given dictionary's tuple pair items = student.items() print('Original week list:', items)
Output
The actual Student Details: {'studentName': 'Reema', 'Roll No': '15cs1029'} Student details after updation: {'age': 22, 'studentName': 'Reema', 'Roll No': '15ME1028'} Student Name: Reema Roll No: 15ME1028 Original week list: dict_items([('age', 22), ('studentName', 'Reema'), ('Roll No', '15ME1028')])
Example 3
# Python program explaining # the dictionary methods # initializing the week dictionary week = { 'Monday': 1, 'Tuesday': 2, 'wednesday': 3 ,'Thursday':4,'Friday':5} print("Actual dictionary:",week) # the dictionary.keys() method # Returning a list containing the dictionary's keys items = week.keys() print('Original week list:', items) # the dictionary.copy() method # copying the elements of dictionary in dictionary1 dictionary1 = week.copy() print("After copying the dictionary elements in dictionary1...") print('dictionary1: ', dictionary1) # the dictionary.clear() method # clearing all the elements week.clear() print('Removing all the element using clear()') print('Week List : ', week) print('dictionary1: ', dictionary1)
Output
Actual dictionary: {'wednesday': 3, 'Friday': 5, 'Tuesday': 2, 'Thursday': 4, 'Monday': 1} Original week list: dict_keys(['wednesday', 'Friday', 'Tuesday', 'Thursday', 'Monday']) After copying the dictionary elements in dictionary1... dictionary1: {'wednesday': 3, 'Friday': 5, 'Tuesday': 2, 'Thursday': 4, 'Monday': 1} Removing all the element using clear() Week List : {} dictionary1: {'wednesday': 3, 'Friday': 5, 'Tuesday': 2, 'Thursday': 4, 'Monday': 1}