Dictionary in Python

Dictionary is a mutable datatype.it is ordered and indexed and written inside curly braces { }.Every element have two things keys and value.

Example
dict={
  "Name"="Vikas",
  "College"="PSIT COE",
  "Roll No"="1934613061"
}

Accessing the element value of dictionary.

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.

Example
dict={
 "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.

Example
dict={
  "Name":"Vikas",
  "College":"PSIT COE",
  "Roll No":"1934613061"
}
print(dict.get["College"])
Output
 "PSIT COE"

Changing the value of any Keys.

We can change the value of any key simply we assign the value using key.

Example
dict={
  "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.

Printing of key using Loop

we can print the keys of dictionary using loop.

Example
dict={
 "Name":"Vikas",
 "College":"PSIT COE",
 "Roll No":"1934613061"
}
for i in dict:
 print(i)
Output
Name
College
Roll No

Printing of value using Loop

we can print the values of dictionary using loop.

Example
dict={
 "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.

Example
dict={
  "Name":"Vikas",
  "College":"PSIT COE",
  "Roll No":"1934613061"
}
for i in dict.values():
   print(i)
Output
Vikas
PSIT COE
1934613061

Printing Key and value in one time

we can print the values and keys of dictionary using items() function.

Example
 dict={
  "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

Existence of keys in Dictionary

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.

Example
dict={
  "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

Length Dictionary

len() function returns the length of dictionary.

Example
dict={
 "Name":"Vikas",
 "College":"PSIT COE",
 "Roll No":"1934613061"
}
print(len(dict))
Output
 3

Adding element in Dictionary

for adding new key and value in dictionary make a new key and assign the new value of that key.

Example
 dict={
  "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"}

Remove element from Dictionary

pop() function are used to remove the element of Dictionary.

Example
dict={
 "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.

Example
dict={
 "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.

Example
dict={
  "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
{ }

Dictionary Copy

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.

Example
dict1={
  "Name":"Vikas",
  "College":"PSIT COE",
  "Roll No":"1934613061"
}
dict2=dict1.copy()
print(dict2)
Output
{  "Name":"Vikas","College":"PSIT COE","Roll No":"1934613061"}