In this article, we’ll learn what Python dict function is and how to use it to create Python dictionaries.
We’ll be going through multiple practical code examples with proper step by step explanation to better understand how Python dict function works.
After reading this post, you’ll have a thorough understanding of how to properly use Python dict function to create dictionaries.
So let’s just dive right into its practical implementation.
What is Python Dict Function?
This built-in Python function is used to create dictionaries in Python. We’ll be going through 3 ways on how we can create dictionaries using this dict function.
Let’s now practically understand how to use Python dict function.
3 Ways to Use Python Dict Function (Easy Examples)
There are 3 easy ways through which we can use this Python dict function to create dictionaries. These are listed below:
- dict(**kwarg)
- dict(mapping,**kwarg)
- dict(iterable,**kwarg)
Using dict(**kwarg)
dictItems=dict(color='green',age=19) print(dictItems)
Output
{'color': 'green', 'age': 19}
You can see that by using kwarg method, we’ve created a Python dictionary. Using this way, we just have to use the assignment operator as shown in the above code.
We have created a new variable and assign that newly created dictionary to it. And finally, we’ve printed it.
As you can see in the output section that the keys are set to string. So make sure that when using kwarg method, use characters or words before the assignment operator without single or double quotes.
You can also implement a nested dictionary using this above method. See below code:
dictItems=dict(color='green',age=19,values={'a':5,'b':'purple'}) print(dictItems)
Output
{'color': 'green', 'age': 19, 'values': {'a': 5, 'b': 'purple'}}
Lists, sets and tuples can also be added to Python dictionary by following this above process.
Using dict(mapping,**kwarg)
Mapping can be done by using only dictionary. Let’s use mapping(not using kwarg for now). See below code:
dictItems=dict({'color':'green','age':19}) print(dictItems)
Output
{'color': 'green', 'age': 19}
Dictionary is created using the mapping way. We can add items to it using kwarg. See below code:
dictItems=dict({'color':'green','age':19}, hobby='Football') print(dictItems)
Output
{'color': 'green', 'age': 19, 'hobby': 'Football'}
Using dict(iterable,**kwarg)
Let’s use list as an iterable to this Python dict function(not using kwarg for now). See below code:
dictItems=dict([('a',34),('b','Green')]) print(dictItems)
Output
{'a': 34, 'b': 'Green'}
We have used two sets in the list as seen in the above code. As a result, we got a dictionary generated from list. We can also use kwarg to add items to it. See below code:
dictItems=dict([('a',34),('b','Green')], c=45.6) print(dictItems)
Output
{'a': 34, 'b': 'Green', 'c': 45.6}
So this is how you can use Python dict function to create dictionaries in your own code as well.
Feel free to ask if you still have any questions regarding the implementation of Python dict function. I’ll be delighted to answer all your questions.
Conclusion
To conclude this post, hope you now have a proper practical knowledge of how to use Python dict function to create dictionaries. Do comment your valuable thoughts on this post. Thank you for reading it.