Python - K List Dictionary Mesh


The term dictionary Mesh is defined by storing the value pair of every key set to dictionary. To include the K list in the dictionary means to insert the list as value pair. In Python, we have some built-in functions such as defaultdict() and lambda will be used to solve the K list dictionary Mesh.

Let’s take an example of this.

Given input list,

list1 = [10, 20, 30]
list2 = [40, 50]

The final output becomes:

{10: {40: [], 50: []}, 20: {40: [], 50: []}, 30: {40: [], 50: []}}

Syntax

The following syntax is used in the examples-

lambda

The function lambda offers a shortcut for declaring brief anonymous functions using the lambda keyword. The lambda function only contains simple expressions.

defaultdict()

The defaultdict() is an in-built function in Python that creates a simple dictionary, but it is initialized with a collection module.

Example 1: Using nested for loop

In the following example, we will start the program by storing the two list values in the variables- list1 and list2 respectively. Then store the empty dictionary in the variable K that will create the result in the form of a dictionary. Then use for loop where variable i iterate through the first list i.e. list1 and set the elements as key and value as {}. Next, use the another for loop that iterates through list2 and set the 2d array as value to the first list, and display the output.

Example

list1 = [3, 2]
list2 = [30, 20]
K = {}
for i in list1:
    K[i] = {}
    for j in list2:
        K[i][j] = []
print("Result of Dictionary Mesh:\n", K)

Output

Result of Dictionary Mesh:
 {3: {30: [], 20: []}, 2: {30: [], 20: []}}

Example 2: Using dictionary comprehension and nested loop

In the following example, start the program by creating two lists of the respective variables. Then use the dictionary comprehension where an empty list and two nested for loops iterate through both the input list to generalize the result.

Example

list1 = [1, 2, 3, 4]
list2 = [100, 200]
K = {i: {j: [] for j in list2} for i in list1}
print("Result of Dictionary Mesh:\n", K)

Output

Result of Dictionary Mesh:
 {1: {100: [], 200: []}, 2: {100: [], 200: []}, 3: {100: [], 200: []}, 4: {100: [], 200: []}}

Example 3: Using defaultdict from the collections module

In the following example, we will start the program by importing the module defaultdict from the library collections that allow users to set default values as keys. Then create the two input lists in the variables- list1 and list2. Next, define the built-in functions- defaultdict() and lambda to calculate the mesh of dictionaries. Moving ahead to use the nested for loop that iterates over both the input list and find the result.

Example

from collections import defaultdict
list1 = [10, 20, 30, 40, 50]
list2 = [5, 6, 7]
K = defaultdict(lambda: defaultdict(list))
for i in list1:
    for j in list2:
        K[i][j] = []
print(dict(K))

Output

{10: defaultdict(<class 'list'>, {5: [], 6: [], 7: []}), 20: defaultdict(<class 'list'>, {5: [], 6: [], 7: []}), 30: defaultdict(<class 'list'>, {5: [], 6: [], 7: []}), 40: defaultdict(<class 'list'>, {5: [], 6: [], 7: []}), 50: defaultdict(<class 'list'>, {5: [], 6: [], 7: []})}

Conclusion

We discussed the dictionary mesh to explore the various methods to solve this problem statement. This program allows the user to match the corresponding elements from an individual list and make key-value in the dictionary. Therefore, this is an easier way to structurize the relatable data.

Updated on: 17-Aug-2023

51 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements