Home > Backend Development > Python Tutorial > what is the only mapping type in python

what is the only mapping type in python

(*-*)浩
Release: 2019-06-24 16:52:29
Original
9302 people have browsed it

Dictionary is the only mapping type in python, which stores data in the form of key-value pairs. Python performs a hash function operation on the key, and determines the storage address of the value based on the calculation result, so the dictionary is stored out of order, and the key must be hashable. Hashable means that the key must be an immutable type, such as numbers, strings, and tuples.

what is the only mapping type in python

Dictionary is the most flexible built-in data structure type in python except lists. A list is an ordered combination of objects, and a dictionary is an unordered collection of objects. The difference between the two is that the elements in the dictionary are accessed by key, not by offset. (Recommended learning: Python video tutorial)

Dictionary is another mutable container model and can store any type of object.

Each key-value key=>value pair in the dictionary is separated by colon :. Each key-value pair is separated by comma,. The entire dictionary is included in curly braces {}. The format is as follows:

d = {key1 : value1, key2 : value2 }
Copy after login

The key is generally unique. If the last key-value pair is repeated, the previous one will be replaced. The value does not need to be unique.

>>>dict = {'a': 1, 'b': 2, 'b': '3'}
>>> dict['b']'3'
>>> dict{'a': 1, 'b': '3'}
Copy after login

The value can be of any data type, but the key must be immutable, such as string, number or tuple.

A simple dictionary example:

dict = {'Alice': '2341', 'Beth': '9102', 'Cecil': '3258'}
Copy after login

You can also create a dictionary like this:

dict1 = { 'abc': 456 }
dict2 = { 'abc': 123, 98.6: 37 }
Copy after login

For more Python-related technical articles, please visit the Python Tutorial column Get studying!

The above is the detailed content of what is the only mapping type in python. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template