close

How to initialize a Python Dictionary

Hello guys. How are you all? I hope you all fine. In this tutorial we will learn about how to initialize a Python Dictionary. so without wasting time lets learn about of this.

How to initialize a Python Dictionary

  1. initialize a Python Dictionary

    To initialize a Python Dictionary Use fromkeys() you can initialize a python dictionary. Lets about of this by this below example.dict1 = dict.fromkeys(['one', 'two', 'three']) print(dict1)
    Output: {'one': None, 'two': None, 'three': None}

  2. How to initialize a Python Dictionary

    To initialize a Python Dictionary Use for loop you can initialize a python dictionary. Lets about of this by this below example. food = ["pizza", "burger", "momos"] my_dic = { i : 1 for i in food} print(my_dic) Output : {'pizza': 1, 'burger': 1, 'momos': 1}

  3. python dictionary initialize

    To initialize a Python Dictionary Use fromkeys() you can initialize a python dictionary. Lets about of this by this below example.dict1 = dict.fromkeys(['one', 'two', 'three']) print(dict1)
    Output: {'one': None, 'two': None, 'three': None}

Method 1 : Using fromkeys()

By using fromkeys() you can initialize dictionary. Lets about of this by this below example.

dict1 = dict.fromkeys(['one', 'two', 'three'])
print(dict1)

Output:

{'one': None, 'two': None, 'three': None}

Method 2 : Using for loop

By using for loop you can initialize dictionary. Lets about of this by this below example.

food = ["pizza", "burger", "momos"]
my_dic = { i : 1 for i in food}
print(my_dic)

Output :

{'pizza': 1, 'burger': 1, 'momos': 1}

Method 3 : Using dict()

By using dict() you can initialize a dictionary. Lets about of this by this below example.

dict1 = dict(rollno1=45,rollno2 = 48,rollno3 = 38)
print(dict1)

Output :

{'rollno1': 45, 'rollno2': 48, 'rollno3': 38}

Conclusion

It’s all About this Tutorial. Hope all methods helped you a lot. Comment below Your thoughts and your queries. Also, Comment below which method worked for you?

Also, Read

Leave a Comment