...

/

Basic Dictionary Operations

Basic Dictionary Operations

Learn about basic dictionary operations in Python.

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, delete
courses['CS444'] = 'Web Services' # add new key-value pair
courses['CS201'] = 'OOP Using java' # modify value for a key
del(courses['CS102']) # delete a key-value pair
del(courses) # delete dictionary object
print(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. ...