1. Accessing Dictionary Items :
Using Keys :
Python
student = { "name": "Ram", "age": 21, "course": "B.Tech" } print(student['name']) #output Ramget() method :
Python
student = { "name": "Ram", "age": 21, "course": "B.Tech" } print(student.get("age")) #output 21keys() 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 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
del :
Deletes the key value pair permanently
popitem( ) :
Removes and returns the last inserted key–value pair.
clear( ) :
Removes all dictionary items but keeps the dictionary object.
4. Copy Dictionary :
copy( ) method :
using dict( ) method :
built-in method to make copy of a dictionary