A variety of common Python dictionary Application Methods

Source: Internet
Author: User

There are many simple functional applications in the Python programming language, but they are very powerful in actual programming. For example, the application of the Python dictionary is one of the most important applications. Next, we will give a detailed introduction to the Python dictionary application method.

Python dictionary application method 1) get (key, default = None)

Returns the value corresponding to the key. If the key is not in the dictionary, the default value is returned. The default value is None.

 
 
  1. >>> Dict1 # empty dictionary
  2. {}
  3. >>> Dict1.get ('A') # key 'A' does not exist in dict1, and none is returned.
  4. >>> Dict1.get ('d1 ', 'no1') # The default parameter provides the value 'no1', so 'no1' is returned'
  5. 'No1'
  6. >>> Dict1 ['a'] = 'no1' # Insert a new element
  7. >>> Dict1
  8. {'A': '000000 '}
  9. >>> Dict1.get ('A') # The current key 'A' exists and its value is returned.
  10. '123'

2) clear

Clear dictionary

3) has_key (key)

If the key appears in dict, True is returned; otherwise, False is returned.

 
 
  1. >>> dict1  
  2. {'a': '1111'}  
  3. >>> dict1.has_key('b')  
  4. False  
  5. >>> dict1.has_key('a')  
  6. True 

4) items

The main function of this Python dictionary application is to return the dict key and value) a list of tuple pairs.

 
 
  1. >>> dict1  
  2. {'a': 'no1', 'b': '2222'}  
  3. >>> dict1.items()  
  4. [('a', 'no1'), ('b', '2222')] 

5) keys returns the dict Key List

6) values returns the dict Value List

 
 
  1. >>> dict1  
  2. {'a': 'no1', 'b': '2222'}  
  3. >>> dict1.keys()  
  4. ['a', 'b']  
  5. >>> dict1.values()  
  6. ['no1', '2222'] 

7) setdefault (key, default = None)

If a key exists in dict, the key value is returned. If no key is found, the key is added to dict. The value is given by the default parameter. The default value is None.

8) update (dict2)

Add the dict2 element to dict. When the key word is repeated, it overwrites the key value in dict.

 
 
  1. >>> Dict2
  2. {'C': '000000', 'B': 'no2 '}
  3. >>> Dict1 # The keys 'B' of dict2 and dict1 are repeated.
  4. {'A': 'no1', 'B': '000000 '}
  5. >>> Dict1.update (dict2) # After update is called, the 'B' VALUE OF THE dict1 key is overwritten.
  6. >>> Dict1
  7. {'A': 'no1', 'C': '000000', 'B': 'no2 '}

9) popitem

This Python dictionary application deletes any key-Value Pair and returns the key-value pair. If the dictionary is empty, an exception occurs.

 
 
  1. >>> dict1  
  2. {'b': 'no2'}  
  3. >>> dict1.popitem()  
  4. ('b', 'no2')  
  5. >>> dict1  
  6. {}  
  7. >>> dict1.popitem()  
  8. Traceback (most recent call last):  
  9. File "< interactive input>", line 1, in < module> 
  10. KeyError: 'popitem(): dictionary is empty' 

10) pop (key, [d])

Delete the key-value pair of the specified key word and return the value corresponding to the key # The second parameter does not know how to use

 
 
  1. >>> dict1  
  2. {'a': 'no1', 'c': '3333', 'b': 'no2'}  
  3. >>> dict1.pop('a')  
  4. 'no1'  
  5. >>> dict1  
  6. {'c': '3333', 'b': 'no2'} 

11) copy

Returns a shortest copy of the dictionary.

The preceding section describes how to use the Python dictionary.

Related Article

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.