write a program in python for following
x = { 'a' : 1 , 'b':2 , 'c':3, 'd':4 }
For the following key,value pairs of dictionary display those which are even values i.e for which the value is even.
i am trying like this but getting confused.
(k:v for k,v in x.items() if v%2 != 0
#Python Code:-
def main():
# Given dictionary
x = { 'a' : 1 , 'b':2 , 'c':3, 'd':4 }
dict1={}
# Traversing dictionary to find out the even values and
then add to new dictionary called dict1
for key,value in x.items():
if value%2==0:
dict1[key]=value
# Printing elements of dictionary having even
value.
print("The items having even value in dictionary: ")
print(dict1)
if __name__=="__main__":
main()
Get Answers For Free
Most questions answered within 1 hours.