Python - Filter odd elements from value lists in dictionary


The dictionary is a popular data type in Python that has a key with value pair and doesn’t allow duplicates. To filter the odd elements it has some built-in functions like items(), filter(), lambda, and, the list() will be used to Filter odd elements from value lists in the dictionary. The odd elements of the list are those elements that are not divisible by 2.

For example

The given list, [10, 22, 21, 19, 2, 5]

After filtering the odd elements from the list:

The final result becomes [10, 22, 2] (These are the elements that are divisible by integer 2).

Syntax

The following syntax is used in the examples −

items()

This is a built-in method that can be used to return the view object. The object consists of a key with value pairs.

filter()

The filter() element of Python is used to filter the elements based on specific conditions.

lambda

The function lambda offers a shortcut for declaring brief anonymous functions using the lambda keyword. The lambda functions behave when declared with the def keyword.

list()

The list() is built-in Python that creates the list object and it accepts an iterable construct and converts it into the list.

x % 2 != 0
or
x % 2 == 1

Both the above representation state the logic to filter the odd elements from the value lists.

Using Dictionary Comprehension and List Comprehension With an if-else Statement

The program uses dictionary comprehension that converts the original dictionary into a new dictionary by filtering the odd elements. This odd element can be filtered by using list compression with an if-else statement.

Example

In the following example, start the program with a function named odd_element that accepts the parameter named dictionary. The same parameters work with the comprehension technique i.e. list and dictionary along with if-statement to set the filter of odd elements from value lists in the dictionary. Then create the value lists of the dictionary and store them in the variable name called dictionary. Next, use the calling function and pass the parameters name as a dictionary which contains a key with value pair and stores it in the variable filter_dict. Finally, we are printing the result with the help of variable filter_dict.

def odd_elements(dictionary):
   return {key: [x for x in value if x % 2 != 0] for key, value in dictionary.items()}

# Create the dictionary
dictionary = {'A': [2, 4, 16, 19, 17], 'B': [61, 71, 90, 80, 10], 'C': [11, 121, 13, 14, 15]}
filter_dict = odd_elements(dictionary)
print("Filter odd elements from the value lists in dictionary:\n", filter_dict)

Output

Filter odd elements from the value lists in dictionary:
{'A': [19, 17], 'B': [61, 71], 'C': [11, 121, 13, 15]}

Using a for Loop and Filter() With Lambda Function

The program uses a for loop that will iterate through the key and value of the dictionary items using built-in method items(). Then it will use the nested built-in functions like list(), filter(), and lambda to remove the odd elements from the lists in dictionary.

Example

In the following example, we will use the for loop to iterate through the variable dictionary where it contains a key with value lists. To filter the odd element it will use three nested built-in functions i.e. list(), filter(), and, lambda()[This function set the condition as x%2 != 0 which will check whether the given value list integer is divisible by 2 or not] and store it in the variable filtered_dictionary. After filtering the odd element, the value of filtered_dictionary is set in the filtered_dictionary. Then create the dictionary which consists of key with value lists and store it in the variable dictionary. Now this variable set in the parameter of calling function named odd_element() and stored in the variable filter_dict().

def odd_elements(dictionary):
   filtered_dictionary = {}
# for loop
   for key, value in dictionary.items():
# Using filter() with lambda
      filtered_values = list(filter(lambda x: x % 2 != 0, value))
      filtered_dictionary[key] = filtered_values
   return filtered_dictionary
# create the dictionary
dictionary = {'A': [1, 2, 3, 4, 5], 'B': [6, 7, 8, 9, 10], 'C': [11, 12, 13, 14, 15], 'D': [16, 17, 18, 19, 20]}
filter_dict = odd_elements(dictionary)
print("Filter odd elements from the value lists in dictionary:\n", filter_dict)

Output

Filter odd elements from the value lists in dictionary:
 {'A': [1, 3, 5], 'B': [7, 9], 'C': [11, 13, 15], 'D': [17, 19]}

Using a for Loop and List Comprehension

The program uses for loop to iterate through dictionary and key using built-in method items() and then it will use for loop and if-statement at one line that represents the list comprehension.

Example

In the following example, begin the program by defining the function named filter_odd_elements() that accepts the parameter named dictionary to access its values of it. Next, create the empty dictionary in the variable filter_dictionary that will later store the new dictionary as a result. Then it will use the for loop to iterate through each value lists of the dictionary. Moving ahead to use the list comprehension using for and if-statement and stored it in the variable filter_values. Swap the same variable in filter_dictionary[key]. Then return the filter_dictionary which have filtered result without an odd element. Create a dictionary that have value lists and store it in the variable dict. The new variable named f_dictionary stores the recursive function to pass the parameter named dict. Finally, use the print function that accepts the variable f_dictionary to get the result.

def filter_odd_elements(dictionary):
   filter_dictionary = {}
   for key, value in dictionary.items():
# List Comprehension
      filter_values = [x for x in value if x % 2 != 0]
      filter_dictionary[key] = filter_values
   return filter_dictionary
# Creation of dictionary
dict = {'A': [307, 907], 'B': [100, 200], 'C': [110, 120]}
# use the calling function
f_dictionary = filter_odd_elements(dict)
print("Filtration of odd elements from dictionary value list:\n", f_dictionary)

Output

Filtration of odd elements from dictionary value list:
 {'A': [307, 907], 'B': [], 'C': []}

Using Dictionary Comprehension and Filter() With Lambda Function

The program uses dictionary comprehension that helps to convert one dictionary into a new form dictionary. The method named filter() use the lambda function to eliminate the odd elements from value lists of dictionary.

Example

In the following example, we will show how dictionary comprehension uses three method to set the logic based on odd element filter from the value lists and using for loop it will iterate each key and value of the dictionary.

def odd_elements(dictionary):
   return {key: list(filter(lambda x: x % 2 == 1, value)) for key, value in dictionary.items()}

# Create the dictionary
dict_1 = {'I': [1, 2, 3, 4, 5], 'II': [6, 7, 8, 9, 10], 'III': [11, 12, 13, 14, 15]}
filter_dict = odd_elements(dict_1)
print("ODD NUMBER FILTRATION IN DICTIONARY VALUES:\n", filter_dict)

Output

ODD NUMBER FILTRATION IN DICTIONARY VALUES:
 {'I': [1, 3, 5], 'II': [7, 9], 'III': [11, 13, 15]}

Using Dictionary Comprehension and List Comprehension

The program uses the recursive function that returns both the comprehension technique by using the return statement.

Example

In the following example, we will use the recursive function in the program to filter out odd elements from the values of a dictionary and returns a new dictionary with the same keys and filtered values.

def odd_elements(dictionary):
   return {key: [x for x in value if x % 2 == 1] for key, value in dictionary.items()}
# create the dictionary and store the value by odd and even in the list
dictionary = {'list1': [100, 200, 300, 499, 599], 'list2': [699, 799, 899, 900, 1000]}
filter_dict = odd_elements(dictionary)
print("ODD NUMBER FILTRATION IN DICTIONARY VALUES:\n", filter_dict)

Output

ODD NUMBER FILTRATION IN DICTIONARY VALUES:
 {'list1': [499, 599], 'list2': [699, 799, 899]}

Conclusion

We discussed the various ways to solve this problem statement based on filtering odd elements from the value lists in the dictionary. All the above examples mostly use the comprehensive technique that solves the problem within 1-2 lines by using either some method, loop, or, conditional statement. The program purpose is normally used when we want to filter the data by assigning specific conditions.

Updated on: 17-Jul-2023

363 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements