A dictionary in Python

Source: Internet
Author: User

Dictionary (dict)

DIC is a mapping type, consisting of key-value pairs enclosed in {}, and key is unique in Dict. When saving, the only memory address is calculated based on key. The key-value is then saved in this address. This algorithm is called the hash algorithm, so, in the dict stored in the Key-value key must be hash, if you do not understand what is hash, temporarily can remember, can change is not hash, The hash means that it will be immutable. This is required to be able to accurately calculate the memory address.

Known hash (immutable) data types: int, str, tuple, BOOL

Non-hash (variable) data types: list, dict, set

Syntax: {key1:value1, key2:value2 ...}

The data saved by Dict is not saved in the order we added. are stored in the order of the hash table. And the hash table is not continuous. So you can't do the slicing work. It can only get the data in Dict by key, before 3.6. The order of the key-value pairs in the result of printing a dictionary is chaotic, and after 3.6, the order of the key-value pairs is the same as the order of the inputs, but the save time is unordered.

Increase:

1.dict[a key0 = value that does not exist in a dictionary, a new key value pair is added

2.setdefalt (): Sets the default value, when no value is set for a key, key equals the default value, and after Method 1 is set, value is the set value.

Delete:

1.pop (key): Pop similar to list, but List uses subscript, and dict is key

2.del keyword usage with list

3.popitem (): Randomly deletes a key-value pair

4.clear (): Empty, one not left

Modify:

1.dict[an already existing key] = value re-assigns a value to an existing value, overwriting the original value

2.dict1.update (DICT2): The Dict2 in the key value of the update to the DICT1, the same key will be overwritten, no key value pairs will be added

Inquire:

1..dict[an already existing key] when key does not exist in the dict, it will error

2.get (Key,defalt=none): Unlike Method 1, the. Key does not exist when it returns none, and this none can be modified by parameter Defalt

Other related actions:

Example: DIC = {"id": 123, "name": ' Sylar ', "age": "OK": "Kobe"}

1.print (Dic.keys) # Dict_keys ([' id ', ' name ', ' age ', ' OK ') its form is similar to list, but not List,print (type (dic.keys)) result is <class ' Dict_keys ' >, but can be used as a list for a for loop.

For key in Dic.keys:

Print (key)

2.print (Dic.values ()) #同keys基本一样, same usage

For value in Dic.values:

Print (value)

3.print (Dic.items ()) #dict_items ([' id ', ' 123 '), (' Name ', ' Sylar '), (' Age ', ' + '), (' OK ', ' Kobe ')])

For key, value in Dic.items:

Print (key, value)

* A special case: direct traversal of DIC, printing is also key.

For I in DIC:

Print (i)

      

    

  

A dictionary in Python

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.