In this python dictionary program, we will filter even numbers from a dictionary value.
Steps to solve the program
- Take a dictionary in the given format as input.
- Create a function to filter even numbers from a dictionary value.
- Use for loop to iterate over keys and values of the dictionary.
- Use another for loop to iterate over elements in the value list.
- Add keys and value list and such the elements in the value list are even numbers to the other dictionary using an if statement.
- Return the dictionary.
- 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]}