1. Accessing Dictionary Items :

Using Keys :

Python
student = { "name": "Ram", "age": 21, "course": "B.Tech" } print(student['name']) #output Ram


get() method :

Python
student = { "name": "Ram", "age": 21, "course": "B.Tech" } print(student.get("age")) #output 21


keys() method :

                 Returns all the keys

Python
student = { "name": "Ram", "age": 21, "course": "B.Tech" } print(student.keys()) #output dict_keys(['name', 'age', 'course']) 

values() method :

                 Returns all the values

Python
student = { "name": "Ram", "age": 21, "course": "B.Tech" } print(student.values()) #output dict_values(['Rahul', 21, 'B.Tech']) 


items() method :

               Returns the key and values together

Python
student = { "name": "Ram", "age": 21, "course": "B.Tech" } print(student.items()) #output dict_items([('name', 'Rahul'), ('age', 21), ('course', 'B.Tech')]) 


Using Loops :

Python
student = { "name": "Ram", "age": 21, "course": "B.Tech" } for k in student: print(k) #output name age course 
2. Adding Items:

 Add new key - value :

Python
student = { "name": "Rahul", "age": 21 } student["course"] = "B.Tech" print(student) #output {'name': 'Rahul', 'age': 21, 'course': 'B.Tech'} 


 Add multiple Items :

Python
student = { "name": "Rahul", "age": 21 } student.update({ "city": "Chennai", "grade": "A" }) print(student) #output {'name': 'Rahul', 'age': 21, 'course': 'B.Tech', 'city': 'Chennai', 'grade': 'A'} 


3. Remove Items :

pop( ) :

             removes the key and returns the value

Python
student = { "name": "Rahul", "age": 22, "course": "CSE" } value = student.pop("age") print(student) print("Removed:", value) #output {'name': 'Rahul', 'course': 'CSE'} Removed: 22 


del :

             Deletes the key value pair permanently

Python
student = { "name": "Rahul", "age": 22, "course": "CSE" } del student["course"] print(student) #output {'name': 'Rahul' , 'age': 22} 

popitem( ) :

             Removes and returns the last inserted key–value pair.

Python
student = { "name": "Rahul", "age": 22, "course": "CSE" } item = student.popitem() print(student) print("Removed:", item) #output {'name': 'Rahul', 'age': 22} Removed: ('course', 'CSE') 


clear( ) :

             Removes all dictionary items but keeps the dictionary object.

Python
student = { "name": "Rahul", "age": 22, "course": "CSE" } student.clear() print(student) #output {}


4. Copy Dictionary :

copy( ) method :


Python
student = {"name": "Rahul", "age": 22} new_dict = student.copy() print(new_dict) new_dict["age"] = 25 print(student) print(new_dict) #output {'name': 'Rahul', 'age': 22} {'name': 'Rahul', 'age': 25}

using dict( ) method :
                       built-in method to make copy of a dictionary

Python
thisdict = { "brand": "Ford", "model": "Mustang", "year": 1964 } mydict = dict(thisdict) print(mydict) #output