Python program to filter even numbers from a dictionary value.

In this python dictionary program, we will filter even numbers from a dictionary value.

Steps to solve the program
  1. Take a dictionary in the given format as input.
  2. Create a function to filter even numbers from a dictionary value.
  3. Use for loop to iterate over keys and values of the dictionary.
  4. Use another for loop to iterate over elements in the value list.
  5. Add keys and value list and such the elements in the value list are even numbers to the other dictionary using an if statement.
  6. Return the dictionary.
  7. Now pass the input dictionary to the function to print the output.
				
					D1 = {'a':[11,4,6,15],'b':[3,8,12],'c':[5,3,10]}

def even(dictt):
    result = {key: [idx for idx in val if not idx % 2]  
          for key, val in dictt.items()}   
    return result

print(even(D1))
				
			

Output :

				
					{'a': [4, 6], 'b': [8, 12], 'c': [10]}
				
			

access the dictionary key.

find the keys of maximum values in a given dictionary.

Leave a Comment