How to create python dictionary
we can create python dictionary in four ways.
1. Empty dictionary
We can create an empty dictionary. In an empty dictionary, there is no key or value.
Only we have to declare a dictionary variable and simply place the curly braces.
emptyDic = { }
Print( emptyDic )
Output
{ }
2. Dictionary with Integer keys
dic1 = { 1 : ‘One’, 2 : ‘Two’, 3 : ‘Three’, 4 : ‘Four’ }
print( dic1 )
Output
{ 1 : ‘One’, 2 : ‘Two’, 3 : ‘Three’, 4 : ‘Four’ }
3. Dictionary with String keys
dic2 = { ‘Name’ : ‘Ajay’, ‘Class’ : 12, ‘City’ : ‘Indore’ }
print( dic2 )
Output
{ ‘Name’ : ‘Ajay’, ‘Class’ : 12, ‘City’ : ‘Indore’ }
4. Dictionary with mixed keys
dic3 = { 1 : ‘One’, ‘Two’ : 2, ‘Book’ : ‘IP’ }
print( dic3 )
Output
{ 1 : ‘One’, ‘Two’ : 2, ‘Book’ : ‘IP’ }
we have learned 4 ways to create python dictionary.
Important Links: Informatics Practices
What is Python Dictionary
How to Traverse Dictionary in Python
Dictionary functions in Python with examples
What is Python List
Characteristics of Python List
How to create Python List
How to Traverse a List in Python
How to Manipulate List in Python
How to Modify multiple elements in Python List
Slicing in Python List
How to delete elements from List in Python