Python : 6 Different ways to create Dictionaries

How we can create dictionaries by multiple ways in python ?

Dictionary in python is one of the important datatype which is used to store data values in key : value pair.

Syntax of dictionary :

dictionary_name = {key1: value1, key2: value2}

where,

  • key1, key2… represents keys in a dictionary. These keys can be of any data type like string, integer, tuple, etc. But keys needs to be unique.
  • value1, value2… represents values in dictionary. These values can be numbers, strings, list or list within a list etc.
  • key and value is separated by : symbol.
  • key-value pair symbol is separated by , symbol.

Approach-1: Creating Empty Dictionary :

By 2 ways we can create empty dictionary.

Method-1 : By using { } empty brackets :

Let’s see the implementation of it with a program.

#Program :

# Empty dictionary created using empty brackets
covid_case_dict = {}
print(covid_case_dict)
Output :
{ }

Method-2: By using using dict() :

Let’s see the implementation of it with a program.

#Program :

# Empty dictionary created using dict()
covid_case_dict = dict()
print(covid_case_dict)
Output :
{ }

Approach-2: Creating Dictionaries with literals :

By passing key : value pairs literals we can create dictionary.

Let’s see the implementation of it with a program.

#Program :

# dictionary created using key-value literals
covid_case_dict = {"January":100000, "February":200000, "March":300000}
print(covid_case_dict)
Output :
{"January":100000, "February":200000, "March":300000}

Approach-3: Creating Dictionaries by passing parameters in dict constructor :

By creating dictionary constructor and passing  key-value pairs within it also we can create a dictionary.

Let’s see the implementation of it with a program.

#Program :

#dictionary created using dictionary constructor
covid_case_dict = dict(January=100000, February=200000, March=300000)
print(covid_case_dict)
Output :
{"January":100000, "February":200000, "March":300000}

Approach-4: Creating Dictionaries by a list of tuples :

We can create a dictionary by passing  list of tuple within dict constructor.

Let’s see the implementation of it with a program.

#Program :

# list of tuples
list_of_Tuples = [("January",100000), ("February",200000), ("March",300000)]
#dictionary created by passing tuple in dict constructor
covid_case_dict = dict(list_of_Tuples)
print(covid_case_dict)
Output :
{"January":100000, "February":200000, "March":300000}

Approach-5: Creating a Dictionary by a list of keys and initializing all with the same value :

We can create a dictionaries by assigning same vales to all the keys.

Suppose we a dictionary of keys. Let’s see how we can initialize all keys with same value.

#Program :

# list of keys 
covid_case_list = ["Januray", "February", "March"]
# create and Initialize a dictionary 
#use list elements as keys and with same value 100000
covid_case_dict = dict.fromkeys(covid_case_list,100000 )
print(covid_case_dict)
Output :
{"January":100000, "February":100000, "March":100000}

Approach-6: Creating a Dictionary by two lists :

If we have two lists, then also we can create a dictionary. For that we can use the elements of the first list as keys and we can use the elements of the second list as values.

In python there is a zip( ) function which we will be used and it will iterate over two lists parallelly.

Let’s see the implementation of it with a program.

#Program :

# First list, its elements will be used as keys 
covid_case_list1 = ["Januray", "February", "March"]

# Second list, its elements will be used as values
covid_case_list2 = [100000, 200000, 300000]
#two lists are merged using zip() to create dictionary
covid_case_dict = dict( zip(covid_case_list1,covid_case_list2))
print(covid_case_dict)
Output : 
{"January":100000, "February":100000, "March":100000}