xxxxxxxxxx
# welcome to softhunt.net
# Creating a Dictionary
Dictionary = {0: 'Softhunt', 1: '.net',
2:{'i' : 'By', 'ii' : 'Ranjeet', 'iii' : 'Andani'}, 3: 'Greetings'}
print('Initial Dictionary: ', Dictionary)
# Deleting a Key value
del Dictionary[1]
print("\nDeleting a specific key: ", Dictionary)
# Deleting a Key from
# Nested Dictionary
del Dictionary[2]['i']
print("\nDeleting a key from Nested Dictionary: ", Dictionary)
xxxxxxxxxx
my_dict = {31: 'a', 21: 'b', 14: 'c'}
del my_dict[31]
print(my_dict)
xxxxxxxxxx
# define a dictionary
my_dict = {'a': 1, 'b': 2, 'c': 3}
# remove a key-value pair from the dictionary
value = my_dict.pop('b')
print(value) #print as 2
print(my_dict) #print as {'a': 1, 'c': 3}
# remove a key-value pair from the dictionary with a default value
value = my_dict.pop('d', None)
print(value) #print as None
print(my_dict) #print as {'a': 1, 'c': 3}
# remove a key-value pair from the dictionary using the `del` statement
del my_dict['a']
print(my_dict) #print as {'c': 3}
xxxxxxxxxx
squares = {1: 1, 2: 4, 3: 9, 4: 16, 5: 25}
# remove a particular item, returns its value
# Output: 16
print(squares.pop(4))