Sort out the most basic Python dictionary operations and

Source: Internet
Author: User

Sort out the most basic Python dictionary operations and

The dictionary in Python is a key-value ing Data Structure in Python. The following describes how to operate the dictionary elegantly.
1.1 create a dictionary

Python has two ways to create a dictionary. The first is to use curly brackets, and the other is to use the built-in function dict.

>>> info = {}>>> info = dict()

1.2 initialize the dictionary

Python can initialize a dictionary when creating a dictionary.

>>> Info = {"name": 'cold'} >>> info = dict (name = 'cold') # more elegant

Obviously, the second method is more elegant and reduces the input of some special characters, but in some cases, the second method is not competent.

>>> key = 'name'>>> info = { key :'cold'} # {'name':'cold'}>>> info = dict(key = 'cold') # {'key': 'cold'}

Obviously, the second method will cause a bug that is not easy to find.

Another initialization method is to use the fromkeys method of the dictionary to obtain elements from the list as keys and initialize them with the second parameter of the None or 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'], 'linuxzen.com')>>> info{'blog': 'linuxzen.com', 'name': 'linuxzen.com'}

1.3 elegant key value acquisition

The dictionary can obtain the key value in this way.

>>> info = {'name':'cold', 'blog':'linuxzen.com'}>>> info['name']'cold'

However, if a key that does not exist is obtained, a KeyError exception is triggered. The dictionary has a get method. You can use the dictionary get method to obtain the dictionary more elegantly.

>>> info = dict(name= 'cold', blog='www.linuxzen.com')>>> info.get('name')'cold'>>> info.get('blogname')None>>> info.get('blogname', 'linuxzen')'linuxzen'

We can see that the get method does not trigger an exception when obtaining a nonexistent key value. At the same time, the get method receives two parameters, if this key does not exist, the second parameter value is returned. We can see that get is more elegant.
1.4 update/Add

The Python dictionary can use keys as indexes to access, update, and add values.

>>> info = dict()>>> info['name'] = 'cold'>>> info['blog'] = 'linuxzen.com'>>> info{'blog': 'linuxzen.com', 'name': 'cold'}>>> info{'blog': 'linuxzen.com', 'name': 'cold night'}

The update method of the Python dictionary can also be used to update and add a dictionary.

>>> Info = dict (name = 'cold', blog = 'linuxzen. com ') >>> info. update ({'name': 'cold night ', 'blogname': 'linuxzen'}) >>> info {'blog ': 'linuxzen. com ', 'name': 'cold night', 'blogname': 'linuxzen '} >>> info. update (name = 'cold', blog = 'www .linuxzen.com ') # more elegant> info {'blog': 'www .linuxzen.com ', 'name': 'cold ', 'blogname': 'linuxzen '}

The update method of the Python dictionary can use a dictionary to update the dictionary. You can also use parameters to pass the same method as the dict function to update a dictionary. The second feature in the above Code is more elegant, however, similar to dict functions, when a key is a variable, only the literal value is used.
1.5 delete a dictionary

You can call the built-in Python keyword del to delete a key value.

>>> info = dict(name='cold', blog='linuxzen.com')>>> info{'blog': 'linuxzen.com', 'name': 'cold'}>>> del info['name']>>> info{'blog': 'linuxzen.com'}

You can also use the pop method of the dictionary to retrieve a key value and delete it.

>>> info = dict(name='cold', blog='linuxzen.com')>>> info.pop('name')'cold'>>> info{'blog': 'linuxzen.com'}

1.6 other operations

Get all keys

>>> info = dict(name='cold', blog='linuxzen.com')>>> info.keys()['blog', 'name']

Get key, value and loop

>>> info = dict(name='cold', blog='linuxzen.com')>>> for key, value in info.items():...   print key, ':', value... blog : linuxzen.comname : cold

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.