Python Dictionary

 What is dictionary?

Dictionary is a data structure in which we represent a group of object as key value pair.


Indexing & slicing not work.

Insertion order is preserved.

Heterogenous elements are allowed.

Mutable in nature.

Key must be unique but duplicates value are allowed.


#Empty dictionary

var={}

print(type(var))

var=dict()

print(type(var))


#Dictionary with item

var={"Name":"Rishikesh","Age":"25"}

print(var)


#pop is use to delete item from dictionary

var={"Name":"Rishikesh","Age":"25","cast":"obc"}

var.pop("cast")

print(var)

var={"Name":"Rishikesh","Age":"25","cast":"obc"}

print(var.pop("cast"))

print(var)


#get method use to get the value of the key

var={"Name":"Rishikesh","Age":"25","cast":"obc"}

print(var.get("cast"))

print(var)


#clear method is use to clear the dict.

var={"Name":"Rishikesh","Age":"25","cast":"obc"}

(var.clear())

print(var)


#keys & value use to print values 

var={"Name":"Rishikesh","Age":"25","cast":"obc"}

print(var.keys())

print(var.values())


#item method use to check item details in dictionary

var={"Name":"Rishikesh","Age":"25","cast":"obc"}

print(var.items())


#print dictionary in series.

var={"Name":"Rishikesh","Age":"25","cast":"obc"}

for key, values in var.items():

    print(key,values)


#sep is use to change the difference

var={"Name":"Rishikesh","Age":"25","cast":"obc"}

for key, values in var.items():

    print(key,values,sep=" - ") 

Comments