About
Kodeclik is an online coding academy for kids and teens to learn real world programming. Kids are introduced to coding in a fun and exciting way and are challeged to higher levels with engaging, high quality content.
Popular Classes
Scratch Coding
Minecraft Coding
TinkerCAD
Roblox Studio
Python for Kids
Javascript for Kids
Pre-Algebra
Geometry for Kids
Copyright @ Kodeclik 2025. All rights reserved.
Dictionaries are associative arrays mapping keys to values. They are very useful in many Python applications. In this blogpost, we will show you 8 different ways to initialize a Python dictionary.
The first way to initialize a Python dictionary is to give the list of key-value pairs within curly braces. For instance:
This outputs:
This is the most straightforward way to initialize a Python dictionary.
The second way to initialize a Python dictionary is to use the dict() constructor. Note that in the syntax below, you input assignments of values to keys.
The printed dictionary has the same format as before:
We can also use a list of tuples and the dict constructor to make up a dictionary.
The output is:
Note that in the first printed line, the output is a list of tuples. In the second printed line we see a dictionary because it uses the list as input to the dictionary.
A fourth way to initialize a Python dictionary is to create two lists, one containing keys and one containing values, and use the zip() function to create pairs of (key,value) from corresponding entries of the two lists.
The output is:
This method is applicable only if all keys have the same value (which is usually satisfied in some initialization settings). For instance, before you begin grocery shopping, values are zero for all the fruits. fromkeys() is a method applicable to the dict() class.
The output is:
A variant of the above is to use dictionary comprehension to loop through the keys and assign an initial value. See for instance:
The output is as expected:
This approach is a minor variant of the previous approach. We first initialize the dictionary to be an empty dictionary and use list comprehension syntax to set default values for keys.
The output is the same as before:
In all of the above approaches, if you try accessing the dictionary with a non-existent key, you will get an error. For instance if you try:
The output is:
The final method for initializing a dictionary will never give such an error as it allows you to provide default values for non-existent keys. This method does not come with Python as default but comes as part of the collections package. It takes as input a function that returns a default value (here presented in lambda notation):
The output is:
because the lambda function gives the default value as 0.
So there you have it - 8 different ways to initialize a Python dictionary! Which one is your favorite? Learn more about dictionaries in our posts about length of a dictionary, swapping positions of values in a dictionary, and about empty dictionaries in Python.
Also learn how to create a complete object-oriented Python program to create and maintain shopping lists using what you have learnt here!
Interested in more things Python? See our blogpost on Python's enumerate() capability. Also if you like Python+math content, see our blogpost on Magic Squares. Finally, master the Python print function!
Want to learn Python with us? Sign up for 1:1 or small group classes.
mydict = {'Apples': 3, 'Oranges': 2, 'Bananas': 6}
print(mydict)
{'Apples': 3, 'Oranges': 2, 'Bananas': 6}
mydict = dict(Apples=3, Oranges=2, Bananas=6)
print(mydict)
{'Apples': 3, 'Oranges': 2, 'Bananas': 6}
mydict = [('Apples', 3), ('Oranges', 2), ('Bananas', 6)]
print(mydict)
print(dict(mydict))
[('Apples', 3), ('Oranges', 2), ('Bananas', 6)]
{'Apples': 3, 'Oranges': 2, 'Bananas': 6}
mykeys = ['Apples', 'Oranges', 'Bananas']
myvalues = [3,2,6]
print(dict(zip(mykeys,myvalues)))
{'Apples': 3, 'Oranges': 2, 'Bananas': 6}
mykeys = ['Apples', 'Oranges', 'Bananas']
print(dict.fromkeys(mykeys,0))
{'Apples': 0, 'Oranges': 0, 'Bananas': 0}
mykeys = ['Apples', 'Oranges', 'Bananas']
mydict = {x: 0 for x in mykeys}
print(mydict)
{'Apples': 0, 'Oranges': 0, 'Bananas': 0}
mykeys = ['Apples', 'Oranges', 'Bananas']
mydict = {}
[mydict.setdefault(x,2) for x in mykeys]
print(mydict)
{'Apples': 2, 'Oranges': 2, 'Bananas': 2}
mydict = {'Apples': 3, 'Oranges': 2, 'Bananas': 6}
print(mydict['Apples'])
print(mydict['Grapes'])
3
Traceback (most recent call last):
File "main.py", line 3, in <module>
print(mydict['Grapes'])
KeyError: 'Grapes'
from collections import defaultdict
mydict = defaultdict(lambda: 0)
print(mydict['Grapes'])
0