Dictionary operations in Python

Source: Internet
Author: User
Tags shallow copy

Dictionary: Dict

Dictionaries are also referred to in other programming languages as associative arrays or hash lists through keys for element access: unordered collections, mutable type containers, variable length, heterogeneous, nested representations: Phonebook = {' Alice ': ' 1234 ', ' Beth ': ' 9102 ',...} A dictionary consists of a key and a corresponding value, a key-value pair called an item. In the previous example, the key is the first name and the value is the phone number. Each key is separated from its value with a colon (:), the item is separated by a comma, and the entire dictionary is enclosed in curly braces {}.dict = {} Defines an empty dictionary dict = {key1:value1,key2:value2,...} Note: In dictionaries (and other mapping types), keys must be unique, if the key conflicts, the last one (can be a hash object can be used as a key, because in the lookup is the key made a hash table to find, so the speed is very fast, the dictionary itself is not a hash, not the dictionary itself when the key, When a dictionary is nested in a dictionary, the internal dictionary can be used as a value instead of as a value, and the values in the dictionary do not need to be. Dict = {' x ': 123, ' Y ': 234, ' Z ': 456}print (dict[' x ']) 123len: Returns the number of elements in the dictionary >>> dict = {' A ': 1, ' B ': 2, ' C ':3}>> > Print (len (dict)) 3 Dictionary copy: dict.copy () deep Copy dict1 = Dict.copy () >>> Dict1 = {' x ': 1, ' Y ': 2, ' Z ':3}>>> Dict2 = Dict1 (shallow copy) >>> ID (DICT1) 140166806717496>>> ID (dict2) 140166806717496>>> dict3 = Dict1.copy () (Deep copy, memory location change) >>> ID (dict3) 140166805562568dict.get (Gets the value of the corresponding key) >>> dict = {' x ': 1, ' Y ': 2, ' Z ':3}>>> dict.get (' x ') 1>>> dict.get (' y ') 2>>> dict.get (' z ') 3>>> >>> Dict.get (' h ') (Get'sBenefits without this element will not throw an exception) >>> Dict.items (the key value in D, the way the dictionary is converted to a tuple list) Note: Each item is split into a tuple that is grouped together to form a list >>> dict = {' X ': 1, ' Y ':2}>>> dict.items () dict_items ([' X ', ' 1 '), (' Y ', 2)]) variable unpacking (both sides to be consistent) >>> Dict = {' x ': 1, ' Y ':2}> >> t1,t2 = Dict.items () >>> print (T1) (' x ', 1) >>> print (T2) (' Y ', 2) >>&gt ; Dict.keys (return key list) >>> Dict = {' x ': 1, ' Y ': 2, ' Z ':3}>>> Dict.keys () Dict_keys ([' X ', ' y ', ' z ']) dict.value ( return value list) >>> dict = {' x ': 1, ' Y ': 2, ' Z ':3}>>> dict.values () dict_values ([1, 2, 3]) Dict.pop (Popup specified key) > >> dict = {' x ': 1, ' Y ': 2, ' Z ':3}>>> dict.pop (' x ') 1>>> print (dict) {' Y ': 2, ' Z ': 3}>>> Dict.popitem (Random popup entry) >>> dict = {' A ': 1, ' B ': 2, ' C ': 3, ' d ': 4, ' E ': 5, ' F ': 6, ' G ':7}>>> dict.popitem (' G ') , 7) >>> Dict.popitem (' F ', 6) >>> Dict.popitem (' E ', 5) >>> Dict.popitem () (' d ', 4) >> > dict.update (Merge a dictionary into the current dictionary) Note: Duplicate keys will be overwritten by the update () method for using the DICT2 The key value pair is added to dict this method does not return any content. >>> Dict1 = {' A ': 1, ' B ':2}>>> dict2 = {' x ': 666, ' A ':888}>>> dict1.update (dict2) >>> Print (Dict1) {' A ': 888, ' B ': 2, ' x ': 666}>>> dict.clear () empty dictionary >>> dict1 = {' A ': 888, ' B ': 2, ' x ': 666}&gt ;>> dict1.clear () >>> dict1{}

Dictionary operations in Python

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.