Home  >  Article  >  Backend Development  >  Can the value of a python dictionary be a dictionary?

Can the value of a python dictionary be a dictionary?

爱喝马黛茶的安东尼
爱喝马黛茶的安东尼Original
2019-06-18 16:55:0410766browse

Dictionary is a data type in Python, characterized by the disorder of elements and the uniqueness of keys. The dictionary creation method is {key: values}. The keys in the dictionary can only be immutable data types (integers, strings or tuples), and the values ​​can be any data type. A set of key:values ​​in a dictionary is called a key-value pair item.

Can the value of a python dictionary be a dictionary?

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

Related recommendations: "Python Video Tutorial"

Each key-value key=>value pair in the dictionary is separated by colon :, between each key-value pair Separate with commas, and the entire dictionary is included in curly braces {}. The format is as follows:

d = {key1 : value1, key2 : value2 }

keys are 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'}

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'}

Construction method for dictionary with value

dic = {}
dic.setdefault(key,{})[value] =1

The construction is as follows:

>>dic.setdefault('b',{})['f']=1
>>dic.setdefault('b',{})['h']=1
>>dic.setdefault('b',{})['g']=1
>>dic
>>{'b': {'h': 1, 'g': 1, 'f': 1}}

The above is the detailed content of Can the value of a python dictionary be a dictionary?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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