Dictionary in Python:-

1) Dictionary Concept in Python:-

Using this we can store elements using key=>value pair, the key is used to provide identity, and value is used to provide store data.


For example, if we store five different student records then roll no will work as a key and rno data 1001  will work as a value.

var = {key:value,key:value}

Stu = {"rno":1001,"sname":"manish kumar","branch":"CS","fees":45000}

rno,sname,branch and fees is the key and 1001,"manish kumar","cs",45000 is the values

................................................................................................................................................................

It will display records based on proper sequence, It is enhancement of set object.

It provides better search and performance as compare to LIST Object.



Dictionary provides a mutable object that means we can add, edit and delete Dictionary Elements Dynamically.


WAP to reverse of Dictionary Elements:-

stu = {"rno":1001,"sname":"manish kumar","branch":"CS","fees":45000}
stu["rno"]=1002
del stu["rno"]
stu.update({"fees":55000})
stu.update({"sem":"vth"})
l = []
for k in stu:
   print("key is ",k ," and value is ",stu[k])
   l.append(k)
for i in range(len(l)-1,-1,-1):
   print("key is ",l[i] ," and value is ",stu[l[i]])


WAP to display dictionary elements in reverse on dictionary format:-

s = {"empid":"Kang1001","empid":"Kang1001","empname":"jay kumar","deptname":"Sales","Salary":45000}

key=[]
val=[]
for d in s:
    key.append(d)
    val.append(s[d])
res= "{"
for i in range(len(key)-1,-1,-1):
   if i!=0: 
    res= res + str(key[i]) + ":" + str(val[i])+","
   else:
    res= res + str(key[i]) + ":" + str(val[i])   

res= res+ "}"

print(res)
for data in s:
    print(data,s[data])


WAP to find max elements in Dictionary?


WAP to  find max key in Dictionary?

WAP to convert dictionary to set?



Post a Comment

If you have any doubt in programming or join online classes then you can contact us by comment .

Previous Post Next Post