This article example describes the common operations of the dictionary of Python foundation. Shared for your reference, as follows:
Python dictionary
A dictionary in Python is a key-value mapped data structure in Python. Here's how to elegantly manipulate dictionaries.
Creating Dictionaries
There are two ways to create a dictionary in Python, the first is to use curly braces, and the other is to use the built-in function dict.
>>> info = {} >>> info = dict()
Initialize Dictionary
Python can initialize a dictionary when it is created
>>> info = {"name" : 'cold'} >>> info = dict(name = 'cold') # more elegant
Obviously, the second method is more elegant and reduces the need for special characters, but there is one case where the second method is not sufficient.
>>> key = 'name' >>> info = { key :'cold'} # {'name':'cold'} >>> info = dict(key = 'cold') # {'key': 'cold'
Obviously the second method raises a bug that is not easy to find
There is another way to initialize a Python dictionary, which is to use the dictionary's fromkeys method to get the elements from the list as keys and initialize them with None or the second argument of the fromkeys method.
>>> info = {}.fromkeys(['name', 'blog']) >>> info {'blog': None, 'name': None} >>> info = dict().fromkeys(['name', 'blog']) >>> info {'blog': None, 'name': None} >>> info = dict().fromkeys(['name', 'blog'], '') >>> info {'blog': '', 'name': ''}
Elegant Fetch Keys
Dictionaries can get the value of a key this way
>>> info = {'name':'cold', 'blog':''} >>> info['name'] 'cold'
But if you get the value of a non-existent key will trigger a KeyError exception, the dictionary has a get method, you can use the dictionary get method more elegant access to the dictionary
>>> info = dict(name= 'cold', blog='') >>> ('name') 'cold' >>> ('blogname') None >>> ('blogname', 'luckycyong') 'luckycyong'
We see that using the get method to get the value of a non-existing key does not trigger an exception, and the get method takes two parameters, and returns the value of the second parameter when the key does not exist. We can see that the use of get is more elegant.
Update/add
Python dictionaries can use keys as indexes to access/update/add values
>>> info = dict() >>> info['name'] = 'cold' >>> info['blog'] = '' >>> info {'blog': '', 'name': 'cold'} >>> info {'blog': '', 'name': 'cold night'}
Also the update method of the Python dictionary can update and add dictionaries
>>> info = dict(name='cold', blog='') >>> ({'name':'cold night', 'blogname':'luckycyong'}) >>> info {'blog': '', 'name': 'cold night', 'blogname': 'luckycyong'} >>> (name='cold', blog='') # More elegant >>> info {'blog': '', 'name': 'cold', 'blogname': 'luckycyong'}
Python dictionary update method can use a dictionary to update the dictionary, you can also use the parameter passing similar to the dict function the same way to update a dictionary, the above code in the oh function of the second more elegant, but also similar to the dict function, the key is a variable, also only take the face value
Dictionary deletion
The Python built-in keyword del can be called to remove a key value
>>> info = dict(name='cold', blog='') >>> info {'blog': '', 'name': 'cold'} >>> del info['name'] >>> info {'blog': ''}
You can also use the dictionary's pop method to take out a key and delete it.
>>> info = dict(name='cold', blog='') >>> ('name') 'cold' >>> info {'blog': ''}
Other operations
Get all keys
>>> info = dict(name='cold', blog='') >>> () ['blog', 'name']
Get key, value and loop
>>> info = dict(name='cold', blog='') >>> for key, value in (): ... print key, ':', value ... blog : name : cold
Readers interested in more Python related content can check out this site's topic: theSummary of Python dictionary manipulation techniques》、《Python list (list) manipulation techniques summarized》、《Summary of Python function usage tips》、《Python Data Structures and Algorithms Tutorial》、《Summary of Python string manipulation techniquesand thePython introductory and advanced classic tutorials》
I hope that what I have said in this article will help you in Python programming.