In this python dictionary program, we will get a list of odd and even keys from the dictionary.
Steps to solve the program
- Take a dictionary as input.
- Use for loop to iterate over the keys in the dictionary.
- Use an if-else statement separate odd and even keys from the dictionary.
- Print the output.
dict1 = {1:25,5:'abc',8:'pqr',21:'xyz',12:'def',2:'utv'}
list1 = [[val,dict1[val]] for val in dict1 if val%2 == 0]
list2 = [[val,dict1[val]] for val in dict1 if val%2 != 0]
print("Even key = ",list1)
print("Odd key = ",list2)
Output :
Even key = [[8, 'pqr'], [12, 'def'], [2, 'utv']]
Odd key = [[1, 25], [5, 'abc'], [21, 'xyz']]