Basic Dictionary Operations
Learn about basic dictionary operations in Python.
We'll cover the following...
Different operations in the dictionary
Some basic dictionary operations are given below.
Mutability
Dictionaries are mutable. So we can perform add, delete, and modify operations on a dictionary.
Press + to interact
courses = { 'CS101' : 'CPP', 'CS102' : 'DS', 'CS201' : 'OOP','CS226' : 'DAA', 'CS601' : 'Crypt', 'CS442' : 'Web'}# add, modify, deletecourses['CS444'] = 'Web Services' # add new key-value paircourses['CS201'] = 'OOP Using java' # modify value for a keydel(courses['CS102']) # delete a key-value pairdel(courses) # delete dictionary objectprint(courses) # We will get an error "NameError: name 'courses' is not defined".
Note: Any new additions will take place at the end of the existing dictionary because dictionaries preserve the insertion order. Dictionary keys cannot be changed in place. ...