Method | Description |
---|---|
clear() | Deletes all the elements from the dictionary. |
copy() | Creates a duplicate copy of the dictionary. |
fromkeys() | Creates a dictionary from the listed keys and their value. |
get() | To get the value of the key. |
items() | It will return tuples of all the items inside the dictionary with keys and values. |
keys() | To get all the keys used inside the dictionary. |
pop() | Deletes item with the specified key from the dictionary. |
popitems() | Removes and returns the last key-value pair from the dictionary. |
setdefaults() | Set and return the dictionary key with the default value. |
update() | Updates the existing key with the new value. |
values() | Returns values of all the keys. |
Python Dictionary clear()
Method
Deletes all the elements from the dictionary.
emp_details = {'first_name':'Tim', 'last_name':'Smith', 'emp_id':200, 'programming_languages':['Python','C++','Java']}
emp_details
emp_details.clear()
emp_details
Python Dictionary copy()
Method
Creates a duplicate copy of the dictionary.
emp_details = {'first_name':'Tim', 'last_name':'Smith', 'emp_id':200, 'programming_languages':['Python','C++','Java']}
emp_details
emp_details_copy = emp_details.copy()
emp_details_copy
Python Dictionary fromkeys()
Method
Creates a dictionary from the listed keys and their value.
key = ('price1', 'price2', 'price3')
value = (1200)
prices = dict.fromkeys(key,value)
prices
Python Dictionary get()
Method
To get the value of the key. If the key is not found in the dictionary, we can return a default value.
emp_details = {'first_name':'Tim', 'last_name':'Smith', 'emp_id':200, 'programming_languages':['Python','C++','Java']}
emp_details
Trying to get the value of last_name.
emp_details.get('last_name')
If the key is not available, then it does not return anything.
emp_details.get('middle_name')
We can set the default value that we want to return in case the key is not available.
emp_details.get('middle_name','N/A')
Python Dictionary items()
Method
It will return tuples of all the items inside the dictionary with keys and values.
emp_details = {'first_name':'Tim', 'last_name':'Smith', 'emp_id':200, 'programming_languages':['Python','C++','Java']}
emp_details.items()
Python Dictionary keys()
Method
To get all the keys used inside the dictionary.
emp_details = {'first_name':'Tim', 'last_name':'Smith', 'emp_id':200, 'programming_languages':['Python','C++','Java']}
emp_details.keys()
Python Dictionary pop()
Method
Deletes item with the specified key from the dictionary.
emp_details = {'first_name':'Tim', 'last_name':'Smith', 'emp_id':200, 'programming_languages':['Python','C++','Java']}
emp_details
Removed ‘last_name’ from dictionary.
emp_details.pop('last_name')
emp_details
Throws a an error if key is not found.
emp_details.pop('middle_name')
emp_details
Python Dictionary popitems()
Method
Removes and returns the last key-value pair from the dictionary.
emp_details = {'first_name':'Tim', 'last_name':'Smith', 'emp_id':200, 'programming_languages':['Python','C++','Java']}
emp_details
emp_details.popitem()
emp_details
Python Dictionary setdefaults()
Method
Set and return the dictionary key with the default value. If the key is not present in the dictionary, will create new key value pair.
emp_details = {'first_name':'Tim', 'last_name':'Smith', 'emp_id':200, 'programming_languages':['Python','C++','Java']}
emp_details
Returns the existing value as the key is already available.
value = emp_details.setdefault('last_name','Wills')
value
Returns the new value as the key is not available.
value = emp_details.setdefault('middle_name','Wills')
value
Observe that last_name is still the same but a new key:value pair for middle_name is added.
emp_details
Python Dictionary update()
Method
Updates the existing key with the new value. If the key does not exist, it will create a new key:value pair.
emp_details = {'first_name':'Tim', 'last_name':'Smith', 'emp_id':200, 'programming_languages':['Python','C++','Java']}
emp_details
‘last_name’ got updated.
emp_details.update({'last_name':'Wills'})
emp_details
‘middle_name’ got added.
emp_details.update({'middle_name':'David'})
emp_details
Python Dictionary values()
Method
Returns values of all the keys.
emp_details = {'first_name':'Tim', 'last_name':'Smith', 'emp_id':200, 'programming_languages':['Python','C++','Java']}
emp_details.values()