What is get() method in dictionary | How to use the get() method of a dictionary.

Definition:

This method is used to get some values from a dictionary by it’s key, meaning we type the key inside get() method and it returns its associated value. Also, if it does not find a matching key it will return None or whatever we specify. 

Syntax:

X = dictionary.get(‘key’)

This method takes 1 argument which is key itself. But we can give another argument after the comma and that would be in string format. That will be used when there is no matching key.

Working:

getmethod() code list dictionary

output getmethod()

You can copy code here.

dictionary = {1: ‘Student’,
‘Name’: ‘Waqar Khan’,
‘Age’: 23,
‘Male’: True,
‘Subjects’: [‘Physics’, ‘Maths’, ‘Computers’]}

x = dictionary.get(‘Name’)
print(x)

Code:

  1. A dictionary with some values is being taken.
  2. get() method is being applied to the dictionary.
  3. Since the get() method returns the output, therefore this output is printed inside x.

Output:

  1. Value of key Name is being printed

Related Post:

Waqar Khan

Hello, everyone, I am Waqar Khan. I have done my B.tech in Computer Science. I love to write about Computer Informational blogs. Before write I did proper research on it. Hope you like our blogs. Thank you.

Related Posts

Leave a Reply

Your email address will not be published. Required fields are marked *