Get value from dictionary by key with get() in PythonIn this tutorial, we will learn how to get the value from dictionary using get() method in Python. How to Create DictionaryA 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.
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:
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. Next TopicHow to convert Hex to ASCII in python |
We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India