Java, .NET, PHP, PYTHON, ANGULAR, ML, Data Science, Testing, CI Tutorials in Easy Languages.

"Best Software Training, Internship, Project Development center of Indore India, Helpline 780506-3968"

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 data.


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

Syntax of the dictionary:-

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 an 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.

Program to display the only key on the Dictionary:-

x = {"rno":1001,"sname":"manish kumar", "branch":"cs", "fees":15000}
for key in x:
    print(key)
    

Program to display the only value on the Dictionary:-

x = {"rno":1001,"sname":"manish kumar", "branch":"cs", "fees":15000}
for key in x:
    print(x[key])
    

Program to display the key and value both on the Dictionary:-

x = {"rno":1001,"sname":"manish kumar", "branch":"cs", "fees":15000}
for key in x:
    print(key,x[key])
    

    
Dictionary provides mutable Object because we can modify(add, edit and delete) dictionary objects?

Example of Dictionary to add, edit and delete elements

x = {"rno":1001,"sname":"manish kumar", "branch":"cs", "fees":15000}
x["rno"]=1002   #modify value
del x["rno"]    #delete key
x.update({'sem':'3rd'}) # append dictionary
for key in x:
    print(key,x[key])
    


How to take input from user's in dictionary Objects?

x = {}
item = int(input("enter number of items in dictionary"))
for i in range(0,item):
    key = input("enter key")
    value = input("enter value")
    x.update({key:value})

print(x)    
    





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 the max key in Dictionary?

Answer:-

d = {"A":12,"C":34,"B":11,"E":78,"F":22}
m =0
for item in d:
    if m<d[item]:
        m=d[item]


print("max value is ",m)        
        


WAP to convert a dictionary to set?



19 Comments

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

  1. Q:- Program to display the only key on the Dictionary ??
    Solution:-
    x = {"Company":"Mahindra","Car Model":"Scorpio S11", "Model Year":2020, "Price":2200000}
    for key in x:
    print(key)

    ReplyDelete
  2. Q:- Program to display the only Value on the Dictionary ??
    Solution:-
    x = {"Company":"Mahindra","Car Model":"Scorpio S11", "Model Year":2020, "Price":2200000}
    for key in x:
    print(x[key])

    ReplyDelete
  3. Q:- Program to display the both key and Value on the Dictionary ??
    Solution:-
    x = {"Company":"Mahindra","Car Model":"Scorpio S11", "Model Year":2020, "Price":2200000}
    for key in x:
    print(key,x[key])

    ReplyDelete
  4. Q:- Program to display the both key and Value on the Dictionary ??
    Solution:-
    x = {"Company":"Mahindra","Car Model":"Scorpio S11", "Model Year":2020, "Price":2200000}
    x["Company"]="Toyota"
    del x["Company"]
    x.update({"Owner Name":"Akshay Ji"})
    for key in x:
    print(key,x[key])

    ReplyDelete
  5. Q:- How to take input from user's in dictionary Objects ??
    Solution:-
    x = {}
    item = int(input("enter number of items in dictionary"))
    for i in range(0,item):
    key = input("enter key")
    value = input("enter value")
    x.update({key:value})

    print(x)

    ReplyDelete
  6. Q:- WAP to find the max key in Dictionary?

    Solution:-
    d = {"A":123,"C":456,"B":789,"E":654,"F":321}
    m =0
    for item in d:
    if m<d[item]:
    m=d[item]
    print("Maximun value is :-",m)

    ReplyDelete
  7. Q:- WAP to display dictionary elements in reverse on dictionary format ??
    Solution:-
    a = {"Company :- ":" Mahindra","Car_Model :- ":" Scorpio S11"," Model_Year :- ": 2020,"Car_Owner :- ":" Akshay Ji","Car_Price :- ": 2200000}

    key=[]
    val=[]
    for b in a:
    key.append(b)
    val.append(a[b])
    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 a:
    print(data,a[data])

    ReplyDelete
  8. #wap to find max key element in dicitonary
    stu={"rno":101,"sname":'anuja',"branch":'cs',"fees":45000}
    rev=[]
    for i in stu:
    print("key is",stu ,"and value is",stu[i] )
    rev.append(i)
    for i in range(len(rev)-1,-1,-1):
    print("key is",rev[i] ,"and value is",stu[rev[i]] )

    ReplyDelete
  9. #find max element in dicitonary
    d={"a":1,"b":20,"c":40,"d":50}
    m=0
    for i in d:
    if m<d[i]:
    m=d[i]
    print("max value is",m)

    ReplyDelete
  10. #wap to convert dictionary to set
    dict={'one':1,'two':2,'three':3,'four':4}
    s=set()
    for i in dict:
    k=(i,dict[i])
    s.add(k)

    print(s)

    ReplyDelete
    Replies
    1. dict={'one':1,'two':2,'three':3,'four':4}
      s=set()
      s1 = set()
      for i in dict:

      s.add(i)
      s1.add(dict[i])

      print(s,s1)

      Delete
  11. Program to convert dictionary to set-->>

    x={"one":1,"two":2,"three":3,"four":4}
    y=set()
    y1=set()

    for i in x:
    y.add(i)
    y1.add(x[i])
    print(y,y1)

    output-->>{'four', 'one', 'two', 'three'} {1, 2, 3, 4}

    ReplyDelete
  12. Program to display dictionary elements in reverse order in dictionary format-->>

    p={"empid":"t04","emp name":"elon musk","department":"space x","salary":"1.5cr"}
    key=[]
    value=[]
    for q in p:
    key.append(q)
    value.append(p[q])
    res="{"

    for i in range(len(key)-1,-1,-1):
    if i!=0:
    res=res+str(key[i])+":"+str(value[i])+","
    else:
    res=res+str(key[i])+":"+str(value[i])

    res=res+"}"
    print(res)

    for info in p:
    print(info,p[info])

    output-->>{salary:1.5cr,department:space x,emp name:elon musk,empid:t04}
    empid t04
    emp name elon musk
    department space x
    salary 1.5cr

    ReplyDelete
  13. Program to del,modify and append in dictionary-->>

    x={"rollno.":234,"name":"jackson","stream":"ec","fees":72500}
    x.update({'sem':'8th'})
    x["rollno"]=2009
    del x["stream"]
    for key in x:
    print(key,x[key])

    output-rollno. 234
    name jackson
    fees 72500
    sem 8th
    rollno 2009

    ReplyDelete
  14. Program to take input from user in dictionary-->>

    p={}
    item=int(input("enter number of items in dictionary"))
    for i in range(0,item):
    key=input("enter key")
    value=input("enter value")
    x.update({key:value})
    print(p)

    ReplyDelete
  15. Program to find max element value in dictionary-->>

    x={"one day":50,"test":200,"t 20":20,"gully cricket":10}
    y=0
    for item in x:
    if y>it is max element- 200

    ReplyDelete
  16. Program to find max key in dictionary-->>

    z= {"a": 100, "b": 121, "c": 144, "d":169, "e":196}
    h=0
    for k in z:
    if h>max key is- e

    ReplyDelete
  17. deependra singh jadaunNovember 28, 2020 at 3:42 PM

    wap to find max. value in dictionary:
    d={'a':1,'b':2,'c':3}
    m=0
    for i in d:
    if (d[i])>m:
    m=d[i]
    print(m)

    ReplyDelete
  18. deependra singh jadaunNovember 28, 2020 at 3:43 PM

    program to take input from users:
    x={}
    d=int(input("enter dictionary no. of items"))
    for i in range (0,d):
    a=input('enter key of dictionary')
    b=input('enter value of dictionary')

    x.update ({a:b})
    print(x)

    ReplyDelete

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