Dictionary is a mutable datatype.it is ordered and indexed and written inside curly braces { }.Every element have two things keys and value.
Exampledict={ "Name"="Vikas", "College"="PSIT COE", "Roll No"="1934613061" }
To access the value of a specific element in a dictionary, you can use its key within square brackets. For instance, if my_dict is your dictionary, my_dict['key'] will return the corresponding value. This allows efficient retrieval of data by directly referencing its associated key.
Exampledict={ "Name":"Vikas", "College":"PSIT COE", "Roll No":"1934613061" } print(dict["Name"])Output
"Vikas"
get() function in Python retrieves the value associated with a specified key in a dictionary. It allows you to provide a default value if the key is not found, preventing errors.
Exampledict={ "Name":"Vikas", "College":"PSIT COE", "Roll No":"1934613061" } print(dict.get["College"])Output
"PSIT COE"
We can change the value of any key simply we assign the value using key.
Exampledict={ "Name":"Vikas", "College":"PSIT COE", "Roll No":"1934613061" } dict["Name"]="Sangam" print(dict)Output
{ "Name":"Sangam", "College":"PSIT COE", "Roll No":"1934613061" }Note:
we can print whole dictionary we just print the name.
we can print the keys of dictionary using loop.
Exampledict={ "Name":"Vikas", "College":"PSIT COE", "Roll No":"1934613061" } for i in dict: print(i)Output
Name College Roll No
we can print the values of dictionary using loop.
Exampledict={ "Name":"Vikas", "College":"PSIT COE", "Roll No":"1934613061" } for i in dict: print(dic[i])Output
Vikas PSIT COE 1934613061
we can print the values of dictionary using values() function.
Exampledict={ "Name":"Vikas", "College":"PSIT COE", "Roll No":"1934613061" } for i in dict.values(): print(i)Output
Vikas PSIT COE 1934613061
we can print the values and keys of dictionary using items() function.
Exampledict={ "Name":"Vikas", "College":"PSIT COE", "Roll No":"1934613061" } for i,j in dict.items(): print(i ,j)Output
Name Vikas College PSIT COE Roll No 1934613061
we can print the values and keys of dictionary using in operator are used to check the keys is belong in the dictionary or not.
Exampledict={ "Name":"Vikas", "College":"PSIT COE", "Roll No":"1934613061" } if "Name" in dict: print(""Name " is present") else: print("Name " is not present")Output
"Name " is present
len() function returns the length of dictionary.
Exampledict={ "Name":"Vikas", "College":"PSIT COE", "Roll No":"1934613061" } print(len(dict))Output
3
for adding new key and value in dictionary make a new key and assign the new value of that key.
Exampledict={ "Name":"Vikas", "College":"PSIT COE", "Roll No":"1934613061" } dict["Branch"]="Information Technology" print(dict)Output
{"Name":"Vikas","College":"PSIT COE","Roll No":"1934613061","Branch":"Information Technology"}
pop() function are used to remove the element of Dictionary.
Exampledict={ "Name":"Vikas", "College":"PSIT COE", "Roll No":"1934613061" } dict.pop("Roll No") print(dict)Output
{"Name":"Vikas","College":"PSIT COE"}
del function are used to delete dictionary.
Exampledict={ "Name":"Vikas", "College":"PSIT COE", "Roll No":"1934613061" } print("Before Delete") print(dict) del dict print("After Delete") print(dict)Output
the above will first prints the dictionary dict with its contents. Then, it deletes the dict variable. Finally, attempting to print dict raises a NameError because the variable is no longer defined.
Before delete
{ "Name":"Vikas","College":"PSIT COE","Roll No":"1934613061"}
After Delete
NameError: name 'dict' is not defined
clear() function are used to delete all element in dictionary.
Exampledict={ "Name":"Vikas", "College":"PSIT COE", "Roll No":"1934613061" } print("Before Clear") print(dict) dic.clear() print("After Clear") print(dict)Output
Before Clear {"Name":"Vikas","College":"PSIT COE","Roll No":"1934613061"} After Clear { }
copy() function in Python creates a shallow copy of a dictionary, allowing you to duplicate its contents into another dictionary variable. This method ensures that modifications to one dictionary do not affect the other. It is a convenient way to duplicate dictionary data for independent manipulation or storage.
Exampledict1={ "Name":"Vikas", "College":"PSIT COE", "Roll No":"1934613061" } dict2=dict1.copy() print(dict2)Output
{ "Name":"Vikas","College":"PSIT COE","Roll No":"1934613061"}