Python

How to Initialize a Dictionary in Python

Dictionaries are quite diverse when it comes to their creation in python code. There are a lot of methods one can find to initialize the dictionary and make use of other data structures in those methods. This article will discuss how to initialize a dictionary data structure in Python language. We have been using Spyder3 for implementations. Let’s start.

Example 01: Initialize an Empty Dictionary

Let’s begin with the first example to create a dictionary. It will be the most simple and basic one, i.e., empty. So, within the Spyder3 tool, we have declared a dictionary variable named “Dic” and assigned no values, i.e., empty. Dictionary is mostly initialized with curly brackets, as shown. The print function is applied to the Dic variable to print its values.

Dic = {}

print(Dic)

Upon running this two-lin code, we have got the empty dictionary as an output below.

Another way to initialize a python dictionary is to use its built-in “dict()” function in the code. So, you have to declare a variable and assign it the “dict()” function as an input value. After this, the same print function is here to print out the initialized dictionary.

Dic = dict()

print(Dic)

After executing the updated code again, we have got the empty dictionary printed out.

Example 02: Initialize with Literals

Another excellent way to initialize a dictionary in python is using the literals. It’s the most common method used to declare and initialize a dictionary. We use this method to declare the keys in inverted double-commas while their values will be separated by the “:” colon sign. Within this example, we have initialized a dictionary name “Dic” with a total of 5 key-pair values. After this, the print statement is used to print the initialized dictionary.

Dic = {"One" : 7, "Two" : 10, "Three" : 45, "Four" : 23, "Five" : 77 }

print(Dic)

After the interpretation and execution, the dictionary initialized with literals is displayed.

Example 03: Initialize by Passing Parameters

Another simple way to initialize a simple dictionary in python is passing values in parameters to a dictionary. Within this method, the assignment operator is used so far. We have declared a library “Dic” and initialized it with 5 keys and 5 values with the help of the assignment operator “=.” At last, the dictionary has been printed out using the print statement.

Dic = dict(One = 70, Two = 45, Three = 75, Four = 83, Five = 9 )

print(Dic)

After running this piece of code, we have got the dictionary printed out in the output screen that has been created by passing the parameters.

Example 04: Listing Only Keys

Within this method, we will look at how to create a dictionary containing the keys and having all the same values. So, we have declared a list containing three strings. We have to use the “fromkeys()” function on the “Dic” list to convert strings to keys. The “dict” function makes the dictionary from those converted keys and saves them to the variable “new.” Now, it is new in the dictionary and printed out as well.

Dic = ["A", "C", "Z"]

new = dict.fromkeys(Dic)

print(new)

After the code execution, you will see that the strings are successfully converted into dictionary keys having the same NULL value to each.

If you don’t want to print NULL as a value to keys of the dictionary, you may use another argument in the “fromkeys()” function. You can see we have given it the value 0.

Dic = ["One", "Two", "Three"]

new = dict.fromkeys(Dic, 0)

print(new)

Now the dictionary values for its keys have been replaced by 0 at all the places.

Example 05: Zip Function on Lists

Another way to initialize and declare a dictionary is using different lists. So, we have started this program with the declaration and initialization of two lists. List1 is a string type, and List2 uses integer values. Zip function utilizes both List1 and List2 in its parameter within the “dict” function to convert list values to keys and values. List1 will be keys, and list2 will be key values. The Dic variable, i.e., converted dictionary, will be printed out on the console.

List1 = ["One", "Two", "Three", "Four", "Five"]

List2 = [1, 2, 3, 4, 5]

Dic = dict(zip(List1, List2))

print(Dic)

You will see a dictionary made by lists on the console screen.

Example 06: Dictionary Comprehension

The dictionary comprehension is quite similar to the list comprehension concept as the code shows that the “List1” shows the keys and the empty list[] shows the value to those keys, i.e., empty. “For” loop is taking a range of 5. The “Dic” variable is printed out as a string dictionary.

Dic = {List1: []for List1 in range(5) }

print("Dictionary:" + str(Dic))

The output shows a dictionary of 5 keys, i.e., 0 to 5 with [] as value to each on running.

The updated code is initialized with a list and replaced [] with a List variable as below.

List = [1, 2, 3]

Dic = {List1: List for List1 in range(5) }

print("Dictionary:" + str(Dic))

A list is displayed as the value to each dictionary key in the output.

Conclusion:

This was all about the creation and initialization of dictionaries in python. The examples we have covered are all simple and easy to implement. We have explained six unique examples for your better understanding. It is recommended to implement all of them

About the author

Kalsoom Bibi

Hello, I am a freelance writer and usually write for Linux and other technology related content

Page was generated in 0.062294960021973