Dictionary get method: Get a value from the dictionary

Source: Internet
Author: User

Obtain a value from the dictionary. 

This article is reproduced in http://blog.sina.com.cn/s/blog_6be89284010183xm.html

Problem:

You need to obtain a value from the dictionary. Do not handle exceptions where the key value you need cannot be found in the dictionary. 

That is, the get method of the dictionary. If you have a dictionary 

Python code
  1. D={'Key': 'value '}  

 



In an exception-safe method, you can write a test to extract the 'key' value from D. 

Python code
  1. If D. has_key ('key '):#Or,InPython2.2OrLater:If'Key'InD:  
  2.   Print D ['key']  
  3. Else:  
  4.   Print 'NotFound'   

 

However, there is a simpler method. 
Print D. Get ('key', 'not
Found ')

Python code
  1. Print D. Get ('key ','NotFound')  

 

Discussion 

I want to get a value from a dictionary, but first make sure that the value is in this dictionary? Use a simple and effective get method. 

If you try to use something like d [x]In that way, the syntax is used to obtain a value, and the value of X is not the key value of dictionary D. Your attempt will throw a keyerror exception. This is often useful. If you expect the value of X to be a key value in D, an exception is the correct way to notify you of mistakes. (That is, you need to debug your program) 

However, you often need more assumptions about it: until you know the value of X or not a key value in D. In this case, forget has_keyMethod or try/retry t statement. Instead, use the get method. If you call D. Get (X), No exception is thrown. If X is a key value of D, you get d [X]. If not, you get none (you can check or spread it ).
If none is not what you want when X is not the key value of D, call D. Get (x, somethingelse). In this case, if X is not a key value. You will get the value of somethingelse 

Get is a simple and effective mechanism, which is well explained in the python documentation. But the number of people who do not know it is surprising. This method is also quite common in Zope when some values in the request dictionary are extracted. 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.