Definition:
Similar to the items() method, this method also returns a view object, this time the view object contains the keys of the dictionary inside list. Also, this view object is directly connected to the original dictionary, meaning any changes in the original dictionary will be reflected here.
Syntax:
X = dictionary.keys()
This method takes no argument.
Working:
Code is here you can copy.
dictionary = {1: ‘Student’,
‘Name’: ‘Waqar Khan’,
‘Age’: 23,
‘Male’: True,
‘Subjects’: ‘Computers’}
x = dictionary.keys()
print(x)
dictionary[‘University’] = ‘MU’
print(x)
Code:
- A dictionary with some values is being taken
- keys() built-in method is being applied to the dictionary.
- Since the keys() method returns the values, the output is being printed in x.
- After output we have inserted a new key and its value in the original dictionary.
- After updating the original dictionary, the value of view object is printed.
Output:
- All the keys of the dictionary before updating its content.
- All the keys after updating its content.