Get the App
SLTechnology News&Howtos  ›  Development  › 

How to get key and value of Dictionary by python

Shulou Source: shulou.com Published: 2022-06-01 02:17:51 09月21日 Update

This article mainly introduces how python obtains the key and value of the dictionary. It is very detailed and has a certain reference value. Interested friends must read it!

Get the key and value of the dictionary

Keys function in Dictionary

The function of the keys function: get all the keys of the current dictionary (key)

The use of the keys function: dict.keys (), which does not need to pass parameters and returns a pseudo-list of key collections

Examples are as follows:

User = {'name':'Neo',' age':18, 'birthday':'2000-01-01'} print (user.keys ()) # the execution result is as follows: # > dict_keys (['name',' age', 'birthday']) # > dict_keys does not have all the functions of the list, cannot get members (elements) through the index, and cannot add, modify, etc.

So how to modify the pseudo-list of dict_keys to make it have the relevant functions of the list? Let's take a look at the following example

User = {'name':'Neo',' age':18, 'birthday':'2000-01-01'} user_list = list (user.keys ()) user_list.append ('sex') print (user_list) # the result is as follows: # > [' name', 'age',' birthday', 'sex'] # in this way, dict_keys has the values function in the dictionary of all the functions in the list

The function of the values function: to get the values of all key-value pairs in the current dictionary (value)

The use of the values function: dict.values (), which does not need to pass parameters and returns a pseudo-list of value collections

Examples are as follows:

User = {'name':'Neo',' age':18, 'birthday':'2000-01-01'} print (user.values ()) # the execution result is as follows: # > dict_values ([' Neo', 18, '2000-01-01]) # > > dict_values does not have all the functions of a list, cannot get members (elements) through the index, and cannot add, modify, etc.

So how to modify the pseudo-list of dict_keys to make it have the relevant functions of the list? Let's take a look at the following example

User = {'name':'Neo',' age':18, 'birthday':'2000-01-01'} user_list = list (user.values ()) user_list.append ('man') print (user_list) # the execution result is as follows: # > [' Neo', 18, '2000-01-01,' man'] # thus, dict_values has all the functions of getting key in the list dictionary

The acquisition method of []

In the index, [] can get the corresponding value of the index; if you add "=" and value, that is to modify the value of the index. The way to get the value through "[]" in the dictionary is the same as the index, as long as the "=" and the value are not written, you are getting the value of the current key.

Dictionary + []: input key in square brackets without assignment, that is, get it.

Returns the value value corresponding to key

Examples are as follows:

User = {'name':'Neo',' age':18, 'birthday':'2000-01-01'} name = user ['name'] print (the value of'\ 'user\' dictionary name is:', name) # the execution result is as follows: # > 'user' dictionary name: Neo

Get acquisition method of Dictionary built-in function

The function of the get function: get the value of the currently specified key

Usage of the get function: dict.get (key,default = Node). Key is the key,default that needs to get value and the default value returned when key does not exist. The default is Node. We can also customize it.

Note:

When we try to use a custom default (dict.get (key, default=' key does not exist'), an error message for TypeError: dict.get () takes no keyword arguments is generated.

Solution:

The get ("key", default=Node) method does not add default=. Deleting this writing does not affect the usage logic, but if it causes an error, we can try to pass in the value or the corresponding variable prompt directly.

Examples are as follows:

User = {'name':'Neo',' age':18, 'birthday':'2000-01-01'} default_info = 'name = user.get (' name', default_info) print (name) # the result is as follows: # > Neouser = {'name':'Neo',' age':18, 'birthday':'2000-01-01'} default_info = 'name = user.get (' sex') Default_info) print (name) # the execution result is as follows: # > the currently passed key does not exist

The difference between [] and get

[] if the obtained key does not exist, an error will be reported directly

The get function returns the default value if the obtained key does not exist

In the process of development, it is recommended to give priority to using the get () function.

The above is all the contents of the article "how to get the key and value of python Dictionary". Thank you for reading! Hope to share the content to help you, more related knowledge, welcome to follow the industry information channel!

Tags: Dictionary function function result index method example example information element content that is member time article face try consistency value interest Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno vpn OPPO Reno Shulou Information MySQL macOS