How to Create Dictionary in Python

Dictionary
  • Dictionary is one of the most important and useful datatype of python programming language 
  • Dictionary pair is a pair of keys and value associated with keys.
 
For Example: 
 
dictionary={"name":"ajay","age":23} 
 
This dictionary haves 2 key and 2 values
 
 
Creating Dictionary
 
Creating dictionary in python is very simple. Just write variable name and assigning key and value within {} brackets.
  1. dictionary={"name":"ajay","age":23}  
Dictionary is created successfully
 
Accessing Dictionary Value
 
In Python's dictionary key is reference for values, means for accessing value we used key as the reference.
  1. dictionary={"name":"ajay","age":23}  
  2. print("name: ",dictionary["name"])  
  3. print("age: ",dictionary["age"])  
Output
 
 
 
Method for getting keys and values
 
In python programming language keys() and values() method is used to get keys and values. 
  • keys()- get all keys from dictionary
  • values()-get all values from dictionary
  1. dictionary={"name":"ajay","age":23}  
  2. print("keys: ",dictionary.keys())  
  3. print("values: ",dictionary.values())  
Output
 
 
Ebook Download
View all
Learn
View all