Program to get a list of odd and even keys from the dictionary

In this python dictionary program, we will get a list of odd and even keys from the dictionary.

Steps to solve the program
  1. Take a dictionary as input.
  2. Use for loop to iterate over the keys in the dictionary.
  3. Use an if-else statement separate odd and even keys from the dictionary.
  4. 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']]
				
			

concatenate two dictionaries

create a dictionary from two lists.

Leave a Comment