Get value from dictionary by key with get() in Python

In this tutorial, we will learn how to get the value from dictionary using get() method in Python.

How to Create Dictionary

A powerful information structure for storing away information in Python is dictionary, in which can recreate the genuine information plan where some particular worth exists for some specific key.

Python Dictionary is utilized to store the information in a key-esteem pair design. It is the impermanent information structure. The components Keys and values is utilized to make the dictionary. Keys should comprise of only one component. Worth can be any sort like rundown, tuple, whole number, and so on.

As such, we can say that a dictionary is the assortment of key-esteem matches where the worth can be of any Python object. Conversely, the keys are the changeless Python object, i.e., Numbers, string, or tuple. Dictionary sections are requested as of Python rendition 3.7. In Python 3.6 and previously, dictionary are for the most part, unordered.

Example:

Output:

<class 'dict'>
printing the slave's data... 
{'Name': 'Sai', 'Age': 20, 'salary': 30000, 'Company': 'Microsoft'}

Dictionary get() Method:

The get() technique in Python is utilized to get the worth of a key from a word reference. On the off chance that the key is absent in the word reference then it basically returns a None. In any case, we can likewise pass a default esteem (alternatively) to the get() strategy in Python. In the event that the key is absent in our word reference then this discretionary default esteem is returned.

Syntax:

Thus, in the get() strategy in Python, we compose our word reference name, trailed by a dot('.') administrator and the get() technique. The get() strategy takes two boundaries - the key and the worth.

Parameters:

As we found in the above syntax, the get() technique in Python takes a key and a discretionary worth.

  • key: The key is a compulsory boundary that is passed to the get() technique in Python. It is essentially the name of the key for which we are looking through the comparing esteem from the word reference. There is plausible that no such key can exist in the word reference. All things considered, our get() strategy in Python basically returns a None.
  • Value: The worth is a discretionary boundary that is passed to the get() technique in Python. Now and again, we probably won't have the predetermined or referenced key in our word reference. In those cases, we can likewise pass a worth alternatively to be returned if no such key exists in the word reference. At long last, the default esteem we have passed as the second boundary of the get() technique is gotten back from our get() strategy.

Return values of get() in Python:

There are three situations with regards to the return upsides of get() strategy in Python. Allow us to examine every one of them:

  • The key as of now exists in the word reference: In the event that the key passed to the get() technique as of now exists in the word reference, then, at that point, the key's comparing esteem is essentially returned by the get() strategy in Python.
  • There is no such thing as the key in the word reference: In the event that the key passed to the get() strategy doesn't exist in the word reference, then None is returned by the get() technique in Python. This adds many advantages to the () technique since no mistake is tossed regardless of whether we are attempting to get to a key that doesn't exist. We likewise have no extra above of placing any really looks at in the word reference.
  • We have passed a discretionary worth to get(): Assume, while bringing any worth from the word reference we have additionally passed any discretionary "esteem" to the get() technique. All things considered, in the event that the key doesn't exists then that specific discretionary worth is returned. In straightforward words, on the off chance that we don't have that specific key in our word reference, then as opposed to returning a None, the get() technique returns the discretionary worth passed to it in the boundary.

Example:

In this example, we will attempt to get the worth of a thing, that doesn't exist in our word reference. So for this, we will take our past model (examined above) where we had a word reference of things (as the keys) and their costs (as their qualities).

Output:

The price of the School bag is = None

Explanation:

In our above model, we are attempting to bring the cost of the thing - pen. In any case, as may be obvious, there is no thing called "pen" in our word reference items_list. Since the key is absent in our word reference, as we previously scholarly over, the get() technique in Python will just return a None, rather than raising a mistake.

Example 2:

In the above model, we passed no default worth to our get() strategy in Python. That was the explanation we were obtain None as the outcome. Presently, in this model, we will be passing a default worth to our get() technique.

Output:

The price of the school bag is = 149.99Rs

Explanation:

As you can see through the above model, we have again attempted to get the worth of the thing pen that doesn't exist in our word reference. Yet, there is one special case in this model, or at least, we have additionally passed the default cost of the thing as the second contention in our get() strategy.

Consequently, in the wake of bringing the thing's worth, we get the default value(we passed to get()) as our outcome and not None. This is likewise a huge benefit of utilizing the get() strategy in Python over the ordinary ordering.






Latest Courses