Definition and operation of the python dictionary

Source: Internet
Author: User

Dictionary features:

The dictionary is unordered. It cannot be accessed through offset, but can only be accessed through keys.

Dictionary = {'key': Value} key: similar to our actual key, while value is a lock. One key opens one lock

Detailed features:

There is no internal order. The key is used to read the content, which can be nested and merged, so that we can organize multiple data structures and modify the content in the same place, which is of a variable type.

The keys that make up the dictionary must be unchangeable data types, such as numbers, strings, tuples, lists, and other variable objects.

1. Create a dictionary dynamically. {}, Dict ()

Info = {'name': 'lilei', 'age': 20}

Info = dict (name = 'lilei', age = 20)

2. Add the inserted content A ['xx'] = 'xx'

For example, info ['phone'] = 'iphone5'

3. Modify the update content A ['xx'] = 'XX ',

Info ['phone'] = 'htc'

The update parameter is a dictionary type that overwrites the value of the same key.

Info. Update ({'city': 'beijing', 'phone': 'nokia '})

HTC becomes Nokia

4. Delete del, clear, and pop.

Del info ['phone'] deletes an element.

Info. Clear () delete all elements of the dictionary

Info. Pop ('name ')

5 In and has_key () member operations to determine whether an element exists.

For example:

1 phone in info

2 info. has_key ('phone ')

6 keys (): The returned list contains all the keys of the dictionary.

Values (): The returned list contains all the values in the dictionary.

Items: the container that generates a dictionary: [()]

7 get: Get a value from the dictionary

Info. Get ('name ')

Info. Get ('age2', '22 ')

Address: http://www.cnpythoner.com/post/246.html

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.