Combining data type Dictionaries

Source: Internet
Author: User

Dict is an unordered combination of data types that contains 0 or more key-value pairs that can be added to or removed from a data item.

Create dictionary D1 = {"id": 1991, "name": "Yoyo", "Size": 6}

>>> D1
{' id ': 1991, ' Size ': 6, ' name ': ' Yoyo '}
>>> d1["id"] #查询
1991

>>> d1["age"] = #添加键值对
>>> D1
{' id ': 1991, ' Size ': 6, ' name ': ' Yoyo ', ' Age ': 25}
>>> del d1["age"] #删除键值对
>>> D1
{' id ': 1991, ' Size ': 6, ' name ': ' Yoyo '}

Dictionary built-in functions

>>> d1.copy () #返回浅拷贝
{' id ': 1991, ' Size ': 6, ' name ': ' Yoyo '}

>>> D1.items () #返回所有键值对的视图
Dict_items ([' ID ', 1991), (' Size ', 6), (' Name ', ' Yoyo ')])

>>> D1.keys () #返回所有键的视图
Dict_keys ([' id ', ' size ', ' name '])

>>> d1.get ("id") #返回相关的值
1991

>>> D1.pop ("id") #返回值并移除相关的项
1991

>>> D1.popitem () #返回并移除任意一个键值对
(' Size ', 6)

>>> d1.values () #返回所有值的视图
Dict_values ([' Yoyo '])

>>> d1.clear () #移除所有项

Combining data type Dictionaries

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.