Python

methods on dictionary in Python

Click for All Topics

methods on dictionary in Python

Here’s a list of all methods that you can apply on dictionary –

dictionary.clear(): Removes all items from the dictionary.

				
					my_dict = {'a': 1, 'b': 2}
print('Original dictionary:',my_dict)
my_dict.clear()
print('Modified dictionary:',my_dict)

# Output -
# Original dictionary: {'a': 1, 'b': 2}
# Modified dictionary: {}
				
			

dictionary.copy(): Returns a shallow copy of the dictionary.

				
					my_dict = {'a': 1, 'b': 2}
copy_of_dict = my_dict.copy()
print('Original dictionary:',my_dict)
print('Copied dictionary:',my_dict)

# Output -
# Original dictionary: {'a': 1, 'b': 2}
# Copied dictionary: {'a': 1, 'b': 2}
				
			

dictionary.fromkeys(iterable, value): Returns a new dictionary with keys from the iterable and values set to the specified value.

				
					keys = ['a', 'b', 'c']
default_value = 0
new_dict = dict.fromkeys(keys, default_value)
print('Original dictionary:',new_dict)

# Output -
# Original dictionary: {'a': 0, 'b': 0, 'c': 0}
				
			
dictionary.get(key, default): Returns the value for the specified key. If the key is not found, it returns the default value.
				
					my_dict = {'a': 1, 'b': 2}
value = my_dict.get('a', 0)
print('value :',value)

# Output -
# value : 1
				
			

dictionary.items(): Returns a view of the dictionary’s key-value pairs as a list of tuples.

				
					my_dict = {'a': 1, 'b': 2}
items = my_dict.items()
print('item of dictionary:',items)

# Output -
# item of dictionary: dict_items([('a', 1), ('b', 2)])
				
			

dictionary.keys(): Returns a view of the dictionary’s keys as a list.

				
					my_dict = {'a': 1, 'b': 2}
keys = my_dict.keys()
print('keys of dictionary:',keys)

# Output -
# keys of dictionary: dict_keys(['a', 'b'])
				
			

dictionary.values(): Returns a view of the dictionary’s values as a list.

				
					my_dict = {'a': 1, 'b': 2}
values = my_dict.values()
print('values of dictionary:',values)

# Output -
# values of dictionary: dict_values([1, 2])
				
			

dictionary.pop(key, default): Removes the specified key and returns its value. If the key is not found, it returns the default value (or raises a KeyError if no default is provided).

				
					my_dict = {'a': 1, 'b': 2}
print('Original dictionary:',my_dict)
value = my_dict.pop('a', 0)
print('Original dictionary:',my_dict)

# Output -
# Original dictionary: {'a': 1, 'b': 2}
# Original dictionary: {'b': 2}
				
			

dictionary.popitem(): Removes and returns the last key-value pair as a tuple.

				
					my_dict = {'a': 1, 'b': 2}
last_item = my_dict.popitem()
print('last item of dictionary:',last_item)

# Output -
# last item of dictionary: ('b', 2)
				
			

dictionary.setdefault(key, default): Returns the value for the specified key. If the key is not found, it inserts the key with the specified default value.

				
					my_dict = {'a': 1, 'b': 2}
print('Original dictionary:',my_dict)
value = my_dict.setdefault('c', 0)
print('Modified dictionary:',my_dict)

# Output -
# Original dictionary: {'a': 1, 'b': 2}
# Modified dictionary: {'a': 1, 'b': 2, 'c': 0}
				
			
dictionary.update(iterable/another_dict): Updates the dictionary with elements from another iterable or dictionary.
				
					my_dict = {'a': 1, 'b': 2}
print('Original dictionary:',my_dict)
my_dict.update({'c': 3, 'd': 4})
print('Modified dictionary:',my_dict)

# Output -
# Original dictionary: {'a': 1, 'b': 2}
# Modified dictionary: {'a': 1, 'b': 2, 'c': 3, 'd': 4}