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:-
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]])
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?
Q:- Program to display the only key on the Dictionary ??
ReplyDeleteSolution:-
x = {"Company":"Mahindra","Car Model":"Scorpio S11", "Model Year":2020, "Price":2200000}
for key in x:
print(key)
Q:- Program to display the only Value on the Dictionary ??
ReplyDeleteSolution:-
x = {"Company":"Mahindra","Car Model":"Scorpio S11", "Model Year":2020, "Price":2200000}
for key in x:
print(x[key])
Q:- Program to display the both key and Value on the Dictionary ??
ReplyDeleteSolution:-
x = {"Company":"Mahindra","Car Model":"Scorpio S11", "Model Year":2020, "Price":2200000}
for key in x:
print(key,x[key])
Q:- Program to display the both key and Value on the Dictionary ??
ReplyDeleteSolution:-
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])
Q:- How to take input from user's in dictionary Objects ??
ReplyDeleteSolution:-
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)
Q:- WAP to find the max key in Dictionary?
ReplyDeleteSolution:-
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)
Q:- WAP to display dictionary elements in reverse on dictionary format ??
ReplyDeleteSolution:-
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])
#wap to find max key element in dicitonary
ReplyDeletestu={"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]] )
#find max element in dicitonary
ReplyDeleted={"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)
#wap to convert dictionary to set
ReplyDeletedict={'one':1,'two':2,'three':3,'four':4}
s=set()
for i in dict:
k=(i,dict[i])
s.add(k)
print(s)
dict={'one':1,'two':2,'three':3,'four':4}
Deletes=set()
s1 = set()
for i in dict:
s.add(i)
s1.add(dict[i])
print(s,s1)
Program to convert dictionary to set-->>
ReplyDeletex={"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}
Program to display dictionary elements in reverse order in dictionary format-->>
ReplyDeletep={"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
Program to del,modify and append in dictionary-->>
ReplyDeletex={"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
Program to take input from user in dictionary-->>
ReplyDeletep={}
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)
Program to find max element value in dictionary-->>
ReplyDeletex={"one day":50,"test":200,"t 20":20,"gully cricket":10}
y=0
for item in x:
if y>it is max element- 200
Program to find max key in dictionary-->>
ReplyDeletez= {"a": 100, "b": 121, "c": 144, "d":169, "e":196}
h=0
for k in z:
if h>max key is- e
wap to find max. value in dictionary:
ReplyDeleted={'a':1,'b':2,'c':3}
m=0
for i in d:
if (d[i])>m:
m=d[i]
print(m)
program to take input from users:
ReplyDeletex={}
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)
#Take input in dictionary
ReplyDeletex={}
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)
#WAP to reverse of Dictionary Elements:-
ReplyDeletea = {}
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]])
#WAP to display dictionary elements in reverse on dictionary format:-
ReplyDeletes = {"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)
# Max number in dictionary
ReplyDeletea = {"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# 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# 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)
#Akash patel
ReplyDelete#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)
Find max elements in dictionary
ReplyDeleteStudents={"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)
#Program to display the only key on the Dictionary?
ReplyDeletex={"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)
s={"rno":1001,"sname":"grv","branch":"cs"}
ReplyDeletel=[]
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]])
x={}
ReplyDeleteitem=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)
x={"hindi":55,"english":59,"maths":88}
ReplyDeletem=0
for i in x:
if m<x[i]:
m=x[i]
print(m)
data={"Rno":101,"Eno":103,"bno":142,"fees":75000}
ReplyDeletex=set()
y=set()
for i in data:
x.add(i)
y.add(data[i])
print(x,y)
x={}
ReplyDeletes=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]])
to find max key element in dicitonary
ReplyDeletes={"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]] )
dic={"eid":1,"ename":"Anku","eadd":"indore"}
ReplyDeletelst=[]
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]])
#Wap to find max and min item in Dictionary?
ReplyDeletedic={"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)
#Wap to take input from user?
ReplyDeletedic={}
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
#WAP to find max elements in Dictionary?
ReplyDeletem=0
x= {"a":12,"b":10,"c":40,"d":49}
for i in x:
if m<x[i]:
m=x[i]
print(m)
#wap to convert dictionary to set?
ReplyDeletesub={"hindi":10,"maths":20,"english":30}
x=set()
x1=set()
for i in sub:
x.add(i)
x1.add(sub[i])
print(x,x1)
#prigram to display the only key on the dicionary:-
ReplyDeletestudent={"rno":1001,"sname":"vani","branch":"cs","fees":20000}
for key in student:
print(key)
#prigram to display the only value on the dicionary:-
ReplyDeletestudent={"rno":1001,"sname":"vani","branch":"cs","fees":20000}
for key in student:
print(student[key])
#example of dictionary to add ,delete,edit elements?
ReplyDeletex={"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)
#wap to find max elements in dictionary?
ReplyDeletes={"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)
#program to display the key and value both on the dictionary:-
ReplyDeletex={"rno":1001,"sname":"kuku","branch":"it","fees":30000}
for data in x:
print(data,x[data])
#program to take input from users:
ReplyDeletex={}
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)
WAP to convert a dictionary to set?
ReplyDeleted = {"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)
emp={'eid':101,'ename':'mangal','dept':'hr'}
ReplyDeletekey=set()
val=set()
for i in emp:
key.add(i)
val.add(emp[i])
print(val)