Dictionary in Python

47



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 store 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 compared 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:-
1)
x = {"rno":1001,"sname":"manish kumar", "branch":"cs", "fees":15000}
for key in x:
    print(key,x[key])
 2)
for k,v in student.items():
      print(k, ' ',v)
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 users 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?
Tags

Post a Comment

47Comments

POST Answer of Questions and ASK to Doubt

  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
  19. #Take input in dictionary
    x={}
    size = int(input("Enter items in dictionary"))

    for i in range(0,size):
    key = input("Enter key")
    value = input("Enter value")
    x.update({key:value})

    print(x)

    ReplyDelete
  20. #WAP to reverse of Dictionary Elements:-
    a = {}
    s = int(input("Enter size of element"))

    for i in range(0,s):
    k = input("Enter key")
    v = input("Enter value")
    a.update({k:v})

    for m in a:
    print(m,a[m])

    p=[]
    for g in a:
    print("key is",g,"and value is",a[g])
    p.append(g)

    for c in range(len(p)-1,-1,-1):
    print("key is",p[c], "and value is ",a[p[c]])

    ReplyDelete
  21. #WAP to display dictionary elements in reverse on dictionary format:-
    s = {"name":"Parag","number":8109220416,"surname":"Jaiswal"}

    key = []
    value = []

    for x in s:
    key.append(x)
    value.append(s[x])

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


    print(b)

    ReplyDelete
  22. # Max number in dictionary
    a = {"x":12,"y": 236,"z":3652,"m":5896}
    key =[]
    value = []
    for i in a:
    key.append(i)
    value.append(a[i])

    print(key,value)

    m = 0
    for num in value:
    if m<num:
    m=num

    print("max no is", m)

    ReplyDelete


  23. # PYTHON ( 6 To 7 PM BATCH)
    # CODE to convert a dictionary to set.

    x1 = {"rno":1001,"sname":"manish kumar", "branch":"cs", "fees":15000}
    a=set()

    for key,value in x1.items():
    a.update((key,value))

    print(type(a),"\n",a)

    ReplyDelete

  24. # Python(6 To 7 PM BATCH)
    # CODE To Find MAX KEY or MAX Value in Dictionary.

    s = {"A":1001,"F":11001,"E":55500,"D":777,"S":45000}
    print("Finding MAX KEY or MAX Value in Dictionary")
    a = str(input("Enter the Option(K for Key and V for Value ):-"))

    if a=="k" or a=="K":
    z = str()
    for i in s:
    if i>z:
    z=i
    print("MAX KEY :-",z)


    if a =="v" or a=="V":
    y = int()
    for i,j in s.items():
    if j>y:
    y=j
    print("MAX Value:-",y)


    ReplyDelete
  25. #Akash patel
    #find max element in dicitonary
    d={"A":10,"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
  26. Find max elements in dictionary

    Students={"ankit":89,"payal":78,"parul":56,"rohit":69}
    Highest=max(students,key=Student.get)
    hmarks=max(student.values())
    Print("student with highest mark is"highest,"scoring"hmarks)

    ReplyDelete
  27. #Program to display the only key on the Dictionary?

    x={"Rno":"101","Sname":"abcd","branch":"cs"}
    for key in x:
    print(key)

    #Program to display the only value on the Dictionary?

    x={"Rno":"101","Sname":"abcd","branch":"cs"}
    for value in x:
    print(x[value])

    #Program to display the key and value both on the Dictionary?

    x={"Rno":"101","Sname":"abcd","branch":"cs"}
    for data in x:
    print(data,x[data])
    #WAP to reverse of Dictionary Elements?

    data= {"rno":101,"sname":"abcd","branch":"EC","fees":75000}
    s = []
    for k in data:
    print("key is ",k ,"& value is ",data[k])
    s.append(k)
    for i in range(len(s)-1,-1,-1):
    print("key is ",s[i] ,"& value is ",data[s[i]])


    #WAP to find max elements in Dictionary?

    m=0
    x= {"x":101,"y":103,"z":524,"A":75}
    for i in x:
    if m<x[i]:
    m=x[i]
    print(m)
    # WAP to convert dictionary to set?

    data={"Rno":101,"Eno":103,"bno":142,"fees":75000}
    x=set()
    y=set()
    for i in data:
    x.add(i)
    y.add(data[i])
    print(x,y)

    ReplyDelete
  28. s={"rno":1001,"sname":"grv","branch":"cs"}
    l=[]
    for k in s:
    print("key is ",k,"value is",s[k])
    l.append(k)
    for i in range(len(l)-1,-1,-1):
    print(l[i],s[l[i]])

    ReplyDelete
  29. x={}
    item=int(input("enter size of dic"))
    for i in range(0,item):
    key=input("enter key")
    value=input("enter value")
    x.update({key:value})
    print(x)

    ReplyDelete
  30. x={"hindi":55,"english":59,"maths":88}
    m=0
    for i in x:
    if m<x[i]:
    m=x[i]
    print(m)

    ReplyDelete
  31. data={"Rno":101,"Eno":103,"bno":142,"fees":75000}
    x=set()
    y=set()
    for i in data:
    x.add(i)
    y.add(data[i])
    print(x,y)

    ReplyDelete
  32. x={}
    s=int(input("enter a number"))
    for i in range(0,s):
    key=input("enter key")
    value=input("enter value")
    x.update({key:value})
    print(x)

    l=[]
    for k in x:
    l.append(k)
    for i in range(len(l)-1,-1,-1):
    print(l[i],x[l[i]])

    ReplyDelete
  33. to find max key element in dicitonary

    s={"rno":101,"sname":'anuja',"branch":'cs',"fees":45000}
    rev=[]
    for i in s:
    print("key is",s ,"and value is",s[i] )
    rev.append(i)
    for i in range(len(rev)-1,-1,-1):
    print("key is",rev[i] ,"and value is",s[rev[i]] )

    ReplyDelete
  34. dic={"eid":1,"ename":"Anku","eadd":"indore"}
    lst=[]
    for key in dic:
    print(key,":",dic[key])
    lst.append(key)

    for i in range(len(lst)-1,-1,-1):
    print(lst[i],":",dic[lst[i]])

    ReplyDelete
  35. #Wap to find max and min item in Dictionary?
    dic={"A":1,"B":7,"C":3,"D":4}
    m=0
    minn=len(dic)
    for i in dic:
    if mdic[i]:
    minn=dic[i]
    print("max item in Dictionary ",m)
    print("min item in Dictionary ",minn)

    ReplyDelete
  36. #Wap to take input from user?
    dic={}
    item=int(input("Enter Item"))
    i=0
    while i<=item:
    key=input("Enter key")
    value=input("Enter value")
    dic.update({key:value})
    print(dic)
    i=i+1

    ReplyDelete
  37. #WAP to find max elements in Dictionary?
    m=0
    x= {"a":12,"b":10,"c":40,"d":49}
    for i in x:
    if m<x[i]:
    m=x[i]
    print(m)

    ReplyDelete
  38. #wap to convert dictionary to set?
    sub={"hindi":10,"maths":20,"english":30}
    x=set()
    x1=set()
    for i in sub:
    x.add(i)
    x1.add(sub[i])
    print(x,x1)

    ReplyDelete
  39. #prigram to display the only key on the dicionary:-
    student={"rno":1001,"sname":"vani","branch":"cs","fees":20000}
    for key in student:
    print(key)

    ReplyDelete
  40. #prigram to display the only value on the dicionary:-
    student={"rno":1001,"sname":"vani","branch":"cs","fees":20000}
    for key in student:
    print(student[key])

    ReplyDelete
  41. #example of dictionary to add ,delete,edit elements?
    x={"rno":1001,"sname":"kuku","branch":"it","fees":30000}
    x.update({"sem":"3rd"}) #append dictionary
    del x["branch"] # delete key
    x["rno"]=1002 #modify value
    for k,v in x.items():
    print(k,v)

    ReplyDelete
  42. #wap to find max elements in dictionary?
    s={"hindi":60,"english":55,"maths":90,"science":80}
    m=0
    for key in s:
    if s[key]>m:
    m=s[key]
    print("max ele",m)

    ReplyDelete
  43. #program to display the key and value both on the dictionary:-
    x={"rno":1001,"sname":"kuku","branch":"it","fees":30000}
    for data in x:
    print(data,x[data])

    ReplyDelete
  44. #program to take input from users:
    x={}
    size = int(input("Enter items in dictionary"))

    for i in range(0,size):
    key = input("Enter key")
    value = input("Enter value")
    x.update({key:value})

    print(x)

    ReplyDelete
  45. WAP to convert a dictionary to set?

    d = {"A":12,"C":34,"B":11,"E":78,"F":22}
    s1=set()
    for k in d:
    s1.add(k)
    s1.add(d[k])
    print(s1)

    ReplyDelete
  46. emp={'eid':101,'ename':'mangal','dept':'hr'}
    key=set()
    val=set()
    for i in emp:
    key.add(i)
    val.add(emp[i])
    print(val)

    ReplyDelete
Post a Comment