Python Basics: Dictionary

Source: Internet
Author: User

About 0x00

Dictionaries (dictionary) are unordered, cannot be accessed by offsets, and can only be read by keys. DIC is a mutable type, but the keys that make up the dictionary must be immutable data types, such as numbers, strings, tuples, and so on.

Syntax: dic = {' key ': value}

0X01 Basic Operations

Two ways to define

>>> ainfo = {'name':'xiaoming'Gender  ':'male'}>>> binfo = dict (name='  Xiaoming', gender='male')

Nesting and modifying

>>> Binfo = {'a': [+],'b': [4,5,6]}#nested lists in dictionaries>>>binfo{'a': [1, 2, 3],'b': [4, 5, 6] }
>>> binfo['a'][2] = 5#value can be modified in-place and is a mutable type>>>binfo{'a': [1, 2, 5],'b': [4, 5, 6]}

Two methods to add

>>> info = {'name':'xiaoming','Gender':'male'}>>> info[' Age'] = 10#single add key and value>>>info{'name':'xiaoming','Gender':'male',' Age': 10}>>> info = {'name':'xiaoming','Gender':'male'}info.update ({' Age': 11,' City':'Shanghai'})#if the key for update already exists, the original value is overwritten, and if key does not exist, it is created>>>info{'name':'xiaoming','Gender':'male',' Age': 11,' City':'Shanghai'}

Three ways to delete

Del

>>>info = {'name':'xiaoming','Gender':'male',' Age': 11,' City':'Shanghai'}>>>delinfo[' City']#just delete the city key>>>delInfo#Delete Info this dic

Clear

>>> a = {'info':'info','age' '=22}>>> a.clear ()

Pop

>>> A = {'name':'Sam',' Age': 22}>>> A.pop (' Age')#Pass in the key that needs to be deleted, return value, and delete from the dictionary22>>>a{'name':'Sam'}>>> A.pop ('Wealth','The key want to pop does not exist') #设置一个默认值 to prompt the Pop object when it does not exist 'The key want to pop does not exist'

Note: When the list uses the Pop method, the index is specified as subscript, such as A.pop (0), and the dictionary specifies the key name when using the Pop method.

Has_key method: Judge a key when it is included in the dictionary

>>>women.has_key ('dick') False

Return a key or value individually as a list

>>> info = {'name':'Jerry' Age '=23}>>> info.keys () ['name','  Age ' ]>>> info.values () ['Jerry', 23]

Items: Creating a container for a dictionary

>>> info = {'name':'Jerry' Age '=23}>>> info.items () [('name':'  Jerry'), (' Age': +)]

Get: Return value by Key name

>>> info = {'name':'Jerry',' Age'=23}>>> Info.get ('name')'Jerry'>>> Info.get ('Gender','Do not exist')#when the key name of get does not exist, return the following sentence'Do not exist'

Python Basics: Dictionary

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.