English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Usage of Multiple Key-Value and Duplicate Key-Value in Python Dictionaries (Detailed Explanation)

The format for using dictionary in Python is as follows:

dict={ key1:value1 , key2;value2 ...}

The format for using when accessing dictionary values in practice is as follows:

dict[key]

Multiple key-value

The format of multiple key-value in dictionary is as follows:

dict={(ke11,key12):value, (key21,key22):value ...}

The specific form of accessing the value in the dictionary in practice is shown as follows (take the first key as an example):

dict[key11,key12]

Or

dict[(key11,key12)]

The following is an actual example:

Multiple values

When a key corresponds to multiple values, the format is:

dict={key1: (value1,value2 ..), key2: (value1,value2 ...) ...}

The format for accessing the value in the dictionary is as follows:

dict[key]

Or

dict[key][index]

Loop assignment (key point)

The syntax structure is shown in the following examples

Summary:

Through the above explanation, it can be known that in the definition of dictionary, the colon ( : ) before and after is an integral part, that is, use parentheses () to enclose the part before and after the colon separately, and it is best to put the key in parentheses as a whole when accessing dictionary values.

Multiple key-value pairs with the same key

That is, in the dictionary, there are at least two members with the same key, but the corresponding values of the keys are different, the format is as follows:

dict={ key1: value1 
    key1: vaklue2,
    ... }

In this form, the value assigned to the key later will become the real value of the key.

Using list and dictionary as the value of dictionary

Format

dict={ key1: (key11:value, key12:value) ,
    key2: (key21:value, key22:value) 
    }

The format for accessing dictionary values (take the first key as an example):

dict[key1], [key11]

The actual example is shown as follows:

This is the full content of the usage method of multiple and duplicate key values in Python dictionary brought to you by the editor. Hope everyone will support and cheer for the tutorial~

You May Also Like