Basic Python Tutorial 4th dictionary: Reading notes when indexing is not good

Source: Internet
Author: User

Fourth dictionary: When the index is not good

1. A data structure that refers to a value by its name , which is called mapping , and the dictionary is the only built-in mapping type in Python.

2.len (): You can return the number of key-value pairs in the dictionary.

The 3.del keyword can also delete items in the dictionary.

4.in can also check if an item exists in the dictionary.

5. The keys in the dictionary can be any immutable data type, such as floating-point, tuple, and string.

6. Dictionaries can also be used to format Strings .

7. Dictionary Method :

①clear (): clears all items in the dictionary.

②copy (): shallow copy a dictionary, feel useless, using the deepcopy function from the copy module may be a little better.

③fromkeys (): Using the given key, create a new dictionary, the second parameter can specify the default value, the default value is None.

④get (): The advantage of accessing a dictionary by giving the specified key is that there is no exception when accessing a nonexistent key . And the second parameter can specify the default value of the value .

⑤items (): Returns all key-value pairs in the dictionary, in the form of a list . Iteritems (): Functions the same, but returns an iterator instead of a list.

⑥keys (): Returns all the keys in the dictionary, in the form of a list . Iterkeys (): Functions, returns an iterator .

⑦values (): Returns all the values in the dictionary, in the form of a list . Itervalues (): Functions, returns an iterator .

⑧pop (): Finds a value by given a key and deletes the key-value pair.

⑨popitem (): randomly pops up a key-value pair and deletes it.

⑩setdefault (): Similar to get (), it can set the corresponding key value if the dictionary does not contain the given key.

⑩①update (): This method can use a different dictionary to update an existing dictionary.

Basic Python Tutorial 4th dictionary: Reading notes when indexing is not good

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.