Create and initialize a Python dictionary
This post will discuss how to create and initialize a dictionary in Python.
A dictionary consists of key-value pairs. There are several ways to create and initialize a dictionary in Python, as discussed below:
1. Using Dictionary Constructor
The dictionary constructor dict() returns a new dictionary that is initialized from the specified arguments.
⮚ If a mapping object is passed to the dictionary constructor, the dictionary is created with the same key-value pairs as the mapping object.
|
1 2 3 4 5 |
if __name__ == '__main__': d = dict(A=1, B=2, C=3) print(d) # {'A': 1, 'B': 2, 'C': 3} |
⮚ You can also pass an iterable to the dictionary constructor, where each item in the iterable must be another iterable with two objects.
This returns a new dictionary with the first object of each item as key and the second object as its corresponding value. Note that if a key is repeated, the resultant dictionary contains the last value for that key.
|
1 2 3 4 5 6 7 8 9 10 11 |
if __name__ == '__main__': x = dict(zip(['A', 'B', 'C'], [1, 2, 3])) print(x) # {'A': 1, 'B': 2, 'C': 3} y = dict([('B', 2), ('A', 1), ('C', 3)]) print(y) # {'A': 1, 'B': 2, 'C': 3} z = dict({'A': 1, 'B': 2, 'C': 3}) print(z) # {'A': 1, 'B': 2, 'C': 3} |
⮚ If no argument is passed to the dictionary constructor, an empty dictionary is created.
|
1 2 3 4 5 |
if __name__ == '__main__': d = dict() print(d) # {} |
2. Using Dictionary Literal Syntax
Dictionaries can also be created by placing a comma-separated list of key: value pairs within braces.
|
1 2 3 4 5 |
if __name__ == '__main__': d = {'A': 1, 'B': 2, 'C': 3} print(d) # {'A': 1, 'B': 2, 'C': 3} |
If no key-value pair is passed, an empty dictionary is created.
|
1 2 3 4 5 |
if __name__ == '__main__': d = {} print(d) # {} |
3. Using fromkeys() function
If you want to initialize all keys in the dictionary with some default value, you can use the fromkeys() function.
|
1 2 3 4 5 |
if __name__ == '__main__': x = dict.fromkeys(['A', 'B', 'C'], 0) print(x) # {'A': 0, 'B': 0, 'C': 0} |
If no default value is specified, the dictionary is initialized with all values as None.
|
1 2 3 4 5 |
if __name__ == '__main__': x = dict.fromkeys(['A', 'B', 'C']) print(x) # {'A': None, 'B': None, 'C': None} |
Note that if the default value is mutable, it might lead to unexpected results. For example, in the following program, all keys get mapped to the same list. This means that any changes made to any value will be reflected in all dictionary’s values.
|
1 2 3 4 5 6 7 |
if __name__ == '__main__': x = dict.fromkeys(['A', 'B', 'C'], []) x.get('A').append(1) print(x) # {'A': [1], 'B': [1], 'C': [1]} |
Read about – defaultdict, OrderedDict
That’s all about creating and initializing a Python dictionary.
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)