The python dictionary exists in the form of "key-value pairs", and all elements are placed in a pair of braces "{}";
The "key" in the dictionary does not allow duplication, and the "value" can be repeated.
The following summarizes the construction methods of 3 dictionaries
1. Direct assignment method
Use = to assign a dictionary to a variable, which means you can create a dictionary variable.
# # Direct assignmenta = {} b = {'a': 1, 2: 'a', 1: 2, "b": 1, '''c''': 1} print('Empty Dictionary:', a, type(a)) print('Dictionary b:', b, type(b))
Empty dictionary: {} <class ‘dict’>
Dictionary b : {'a': 1, 2: 'a', 1: 2, 'b': 1, 'c': 1}<class 'dict'>
Note: Single, double, and triple quotation marks are all possible in the dictionary. This is different from json format data. Json format data can only use double quotation marks.
2. Built-in function dict()
Quickly create dictionary using built-in function dict
**Usage 1: **Create an empty dictionary
# Built-in function dict()a = dict() print(a, type(a))
Result: {} <class ‘dict’>
**Usage 2: **Combining 2 list format data into a dictionary
# Combine 2 list data into a dictionarykeys = ['a', 'b', 'c'] values = [1, 2, 3] dict1 = dict(zip(keys, values)) print('result:', dict1)
Results: {'a': 1, 'b': 2, 'c': 3}
Note: When combining dictionaries using this method, you must ensure that the lengths of the two lists are the same.
**Usage 3: **Create a dictionary based on the given "key-value pair"
# Create a dictionary based on the given key-value pairdict1 = dict(a=1, b=2, c=3, d='a') print('result:', dict1)
Results: {'a': 1, 'b': 2, 'c': 3, 'd': 'a'}
**Usage 4: ** Given content is "key", create a dictionary with "value" empty
# # Given content as a key, create a list with empty valuesdict1 = (['a', 'b', 'c']) dict2 = ({'a', 'b', 'c'}) dict3 = (('a', 'b', 'c')) print('Result 1:', dict1) print('Result 2:', dict2) print('Result 3:', dict3)
Result 1: {'a': None, 'b': None, 'c': None}
Result 2: {'c': None, 'b': None, 'a': None}
Results 3: {'a': None, 'b': None, 'c': None}
Note: The value types in () can be list, collection, or tuple.
**Usage 5: **Create a dictionary with the same "value" corresponding to all "keys"
# Create a dictionary with the same value corresponding to all keysdict1 = (['a', 'b', 'c'], 1) dict2 = (['a', 'b', 'c'], [1,2]) print('Dictionary with all values 1:', dict1) print('Dictionary with all values [1,2]:', dict2)
Dictionary with all values 1: {'a': 1, 'b': 1, 'c': 1}
Dictionary with all values [1,2]: {'a': [1, 2], 'b': [1, 2], 'c': [1, 2]}
3. Dictionary derivation formula
Create a dictionary using dictionary derivation
# Create a dictionary using dictionary derivationdict1 = {key:values for (key, values) in []} dict2 = {key: values for (key, values) in zip(['a', 'b', 'c'], [1, 2, 'a'])} print('Result 1:', dict1) print('Result 2:', dict2)
Result 1: {}
Result 2: {'a': 1, 'b': 2, 'c': 'a'}
The above are common uses for building python dictionaries, and you can choose different uses according to actual needs. For more related python dictionary content, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!