Python Learning notes-dictionaries

Source: Internet
Author: User
Tags delete key shallow copy

The dictionary is the only built-in mapping type in Python.

Create dictionary phonebook = {' Alice ': ' 2341 ', ' Beth ': ' 9102 '}

You can use Dict to build a dictionary from a sequence of other mappings or key-value pairs. Key value parameter to create a dictionary.

The number of items in Len (d) d. D[K] Returns the value associated to the key, d[k] = v assigns the key, Del D[k] the DELETE key k,k in D contains the key K.

Key type, automatically added, membership.

The formatted string of the dictionary.

Method:

1.clear clears all entries in the dictionary. Operate in situ.

2.copy shallow Copy, change the value of the original data has no effect, delete all deleted. Deepcopy deep Copy, no impact.

3.fromkeys creates a new dictionary with the given key, the default value of None

4.get Access Dictionary, if none is returned, there will be no exception.

5.has_key Check if a key value is included in the dictionary

6.items all items are returned in the form of a key-value pair, with no order. Iteritems returns an Iterator object.

The 7.keys list shows all keys, Iterkeys returns an iterator for a key.

8.pop gets the value corresponding to the given key, and then deletes the key-value pair in the dictionary.

9.popitem randomly pops a key value

10.setdefault is similar to the Get method, but it can assign values to non-existent key values.

11.update New Dictionary update old dictionary

12.valus returns the value in the dictionary as a list, itervalues the iterator.

Python Learning notes-dictionaries

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.