Initialize a Python Dictionary

  1. Use the Literal Syntax to Initialize a Dictionary in Python
  2. Use the dict() Constructor to Initialize a Python Dictionary
  3. Use the fromkeys() Method to Initialize a Python Dictionary
  4. Use a List of Tuples to Initialize a Python Dictionary
  5. Use Two Lists to Initialize a Python Dictionary

Python Dictionaries are ordered and changeable. Dictionaries do not allow the storage of duplicates.

In the versions Python 3.6 and below, dictionaries used to be unordered. After the introduction of Python 3.7+, dictionaries are ordered.

This tutorial will discuss different methods to initialize a dictionary in Python.

Use the Literal Syntax to Initialize a Dictionary in Python

A dictionary can be created and initialized using curly brackets {}, and it contains keys and values.

The following code uses literals to initialize a Python dictionary.

dict1 = {'X': 2, 'Y': 3, 'Z': 4}
print(dict1)

Output:

{'X': 2, 'Y': 3, 'Z': 4}

Use the dict() Constructor to Initialize a Python Dictionary

The dict() constructor can be used to initialize dictionaries from keyword arguments, or a solitary dictionary and its keyword arguments, or a solitary iterable of key-value pairs.

We can pass parameters in the dict() constructor and create a dictionary.

The following code uses the dict() constructor to initialize a dictionary in Python.

dict1 = dict(X=1, Y=2, Z=3)
print(dict1)

Output:

{'X': 1, 'Y': 2, 'Z': 3}

Use the fromkeys() Method to Initialize a Python Dictionary

The fromkeys() function can be used if all the keys have the same value.

The following code uses the fromkeys() method to initialize a Python dictionary.

dict1 = dict.fromkeys(['X', 'Y', 'Z'], 0)
print(dict1)

Output:

{'X': 0, 'Y': 0, 'Z': 0}

The dictionary can be initialized with values of all the keys as None, if no particular value is specified in the syntax.

dict1 = dict.fromkeys(['X', 'Y', 'Z'])
print(dict1)

Output:

{'X': None, 'Y': None, 'Z': None}

Use a List of Tuples to Initialize a Python Dictionary

A list of tuples can also be used to initialize a dictionary in Python. This method also uses the dict() constructor to implement this.

A tuple is an ordered and immutable collection of objects. It can be utilized to stock multiple items in a single variable.

Lists are similar to tuples, with the only difference being that lists can be changed, and Tuples do not allow that.

The following code uses a list of tuples to initialize a dictionary in Python.

LOT = [("X" , 5), ("Y" , 6), ("Z" , 8)]
dict1 = dict(LOT)
print(dict1)

Output:

{'X': 5, 'Y': 6, 'Z': 8}

Use Two Lists to Initialize a Python Dictionary

In this case, we declare two lists, where the values of the first list are to be used as keys, and the second list is to be used as the values of the dictionary that we will initialize.

To implement the above statements, we can use the zip() function, which iterates over both the given lists in parallel.

The zip() function will create a key-value pair for all the entries in parallel and will successfully create a zipped object, which can then be passed to the dict() constructor to create a dictionary.

The following code uses two lists to initialize a dictionary in Python.

if __name__ == '__main__':
    
    L1 = ["X", "Y", "Z"]
    L2 = [5,6,8]
    dict1 = dict( zip(L1,L2) )
    print(dict1)

Output:

{'X': 5, 'Y': 6, 'Z': 8}

In this case, L1 is the list used as keys, and L2 is used as a list of values for the initialized dictionary.

Contribute
DelftStack is a collective effort contributed by software geeks like you. If you like the article and would like to contribute to DelftStack by writing paid articles, you can check the write for us page.

Related Article - Python Dictionary

  • Remove a Key From a Dictionary in Python
  • Convert a Dictionary to a List in Python