Python

How to Initialize a Dictionary in Python

In Python, dictionaries are a collection of unordered items containing pairs of keys and values. Dictionaries are little bit different when it comes to their creation. More specifically, Python provides several methods and functions used to find and initialize the dictionary, making it usable for other data structures in these methods.

This write-up will discuss the several ways for the initialization of a dictionary in Python.

How to Initialize a Dictionary in Python?

There are multiple methods available in Python that are used to initialize a dictionary. Some of them are listed below:

Method 1: Initialize a Dictionary in Python Using Dictionary Comprehension

The dictionary comprehension is the easiest way to initialize a dictionary in Python that is quite similar to the list comprehension.

Example

To initialize the dictionary, use the “for” loop that takes the range by utilizing the “range()” method:

my_dict = {my_list: [] for my_list in range(3)}

Print the value of initialized dictionary value:

print ("My New Initialized Dictionary: " + str(my_dict))

Output

Method 2: Initialize a Dictionary in Python Using “{}” Braces Method

Another easiest way to initialize the dictionary in Python is using the curly braces “{}” method.

Example

To initialize the empty dictionary, use the “{}” braces:

my_dic = {}

Apply the print statement to the “my_dic” variable to get its value:

print("My New Empty Initialized Dictionary: ", my_dic)

As you can see, the empty list has been initialized successfully:

Method 3: Initialize a Dictionary in Python Using “fromkey()” Method

Use the “fromkey()” method to create and initialize a dictionary from the specified keys sequence and values.

Example

First, declare a list that contains three strings:

keyList = ["Linux", "Hint", "World"]

Specify the number to which we want to initialize with the provided list strings:

num = 3

Call the “fromkeys()” method on the generated list to convert the strings into keys and stores them to the “my_dic” variable:

my_dic = dict.fromkeys(keyList, num)

Use the “print()” function to get the initialized dictionary:

print("My New Initialized Dictionary: " , my_dic)

Output

Method 4: Initialize a Dictionary in Python Using “defaultdict()” Method

The “defaultdict()” method can be utilized to initialize a dictionary in Python. It is a simple way to use any key without initialization of value.

Example

Import the “defaultdict” module from the “collections” library:

from collections import defaultdict

Use the “defaultdict()” method with the list as an argument for initializing the dictionary and save them to the “my_dict” variable:

my_dict = defaultdict(list)

Perform the append operation by utilizing the “append()” method and specify the string:

my_dict[0].append('LinuxHint')

Use the “dict()” method to get the dictionary inside the print statement:

print ("My New Initialized Dictionary: " + str(dict(my_dict)))

Output

Method 5: Initialize a Dictionary in Python Using “setdefault()” Method

To initialize a dictionary in Python, the “setdefault()” method can be used by specifying the key-value pairs within the comprehension.

Example

Initialize the dictionary with an empty list:

my_dict = {}

Invoke the “setdefault()” function with arguments with the “for” loop and specify the range by utilizing the “range()” function:

[my_dict.setdefault(x, []) for x in range(3)]

Utilize the “append()” function with the string as argument:

my_dict[0].append('LinuxHint')

Call the “dict()” function within the print statement to get the initialized dictionary:

print ("My New Initialized Dictionary: " + str(dict(my_dict)))

It can be observed that the dictionary has been initialized successfully:

Method 6: Initialize a Dictionary in Python Using “dict()” and “zip()” Methods

The “dict()” and “zip()” methods are also used for initializing a dictionary in Python. Both are built-in functions that are utilized with the list comprehension to achieve the desired result.

Example

Use the “range()” method by specifying the desired range and storing into the “dic_key

dic_keys = range(3)

Use the “zip()” method with required arguments and provided range with “for” loop inside the “dict()” method:

my_dict = dict(zip(dic_keys, ([] for _ in dic_keys)))

Now, append through the “append()” method with list index:

my_dict[0].append('LinuxHint')

Call the “print()” function:

print("My New Initialized Dictionary: " + str(dict(my_dict))

Output

Method 7: Initialize a Dictionary in Python By Passing Parameters

We can also initialize the dictionary by passing arguments using the “dict()” built-in method.

Example

Use the “dict()” method and pass the values in the parameters to a dictionary:

my_dic = dict(Linux=0, Hint=1, World=2)

Now, get the dictionary using the print statement:

print("My New Initialized Dictionary: ", my_dic)

Output

That’s all! We have provided multiple techniques to initialize a dictionary in Python.

Conclusion

To initialize the dictionary, the “fromkey()”, “defaultdict()”, “setdefault()”, “dict()”, “zip()” methods, passing arguments, curley “{}” braces, and the dictionary comprehension techniques are used. This write-up demonstrated multiple techniques to initialize a dictionary in Python.

About the author

Maria Naz

I hold a master's degree in computer science. I am passionate about my work, exploring new technologies, learning programming languages, and I love to share my knowledge with the world.