Dictionary in Python:-
1) Dictionary Concept 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.
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:-
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]])
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)
Post a Comment
If you have any doubt in programming or join online classes then you can contact us by comment .