Python Advanced Dictionary

Source: Internet
Author: User
The advanced tutorial further expands the basic tutorial to illustrate the details of Python. I hope you will have a more comprehensive understanding of Python after the advanced tutorial. The basic tutorial introduces basic concepts, especially objects and classes.

The advanced tutorial further expands the basic tutorial to illustrate the details of Python. I hope you will have a more comprehensive understanding of Python after the advanced tutorial.

As we mentioned earlier, the list is a class in Python. A specific table, such as nl = [1, 3, 8], is an object of this class. We can call some methods of this object, such as nl. append (15 ).

We will introduce a new class, dictionary ). Similar to the list, a dictionary can store multiple elements. The object that stores multiple elements is called a container ).

Basic concepts

Common ways to create a dictionary:

>>>dic = {'tom':11, 'sam':57,'lily':100}>>>print type(dic)

Similar to a table, a dictionary contains multiple elements separated by commas. However, the dictionary element contains two parts: keys and values. Commonly, keys are represented by strings. you can also use numbers or true values to represent keys (immutable objects can be used as keys ). The value can be any object. Keys and values correspond to each other.

For example, in the above example, 'Tom 'corresponds to 11, 'Sam corresponds to 57, and 'Lily' corresponds to 100

Different from a table, the dictionary elements have no order. You cannot reference an element by subscript. A dictionary is referenced by a key.

>>>print dic['tom']>>>dic['tom'] = 30>>>print dic

Create a new empty dictionary:

>>>dic = {}>>>print dic

Add a new element to the dictionary:

>>>dic['lilei'] = 99>>>print dic

Here, we reference a new key and assign it the corresponding value.

Cyclic call of dictionary elements

dic = {'lilei': 90, 'lily': 100, 'sam': 57, 'tom': 90}for key in dic:    print dic[key]

In a loop, each key of dict is extracted and assigned to the key variable.

With the print result, we can confirm again that the elements in dic are unordered.

Common dictionary methods

>>> Print dic. keys () # return all dic keys> print dic. values () # return all dic values >>> print dic. items () # return all dic elements (key-value pairs)> dic. clear () # clear dic and change dict {}

There is also a common usage:

>>> Del dic ['Tom '] # Delete the 'Tom' element of dic.

Del is a reserved keyword in Python and is used to delete objects.

Similar to a table, you can use len () to query the total number of elements in the dictionary.

>>>print(len(dic))

Summary

Each element in the dictionary is a key-value pair. The element has no order.

dic = {'tom':11, 'sam':57,'lily':100}dic['tom'] = 99for key in dic: ...del, len()

The above is the content of the Python Advanced Dictionary. For more information, see The PHP Chinese website (www.php1.cn )!

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.