user9431057
user9431057

Reputation: 1253

Create a Dictionary of Dictionary from a Tuple of Dictionary Inside a List

I have a tuple like this,

sample_tuple = ([{1:["hello"], 2: ["this"], 3:["is fun"]},{1:["hi"], 2:["how are you"]}],
                [{1: ["this"], 2:[], 3:["that"]}, {1:[], 2:["yes"]}])

From this tuple, I would like to create a dictionary that has its key values as dictionary.

Step 1:

Iterate the main big tuple and keep track of the indexes of lists.

Step 2:

Get into the lists inside the tuple and keep track of the index of those big lists.

i.e, index 0 of first list,

[{1:["hello"], 2: ["this"], 3:["is fun"]},{1:["hi"], 2:["how are you"]}] 

Step 3:

I want to iterate through the key and values of dictionary inside the list.

i.e first dictionary

{1:["hello"], 2: ["this"], 3:["is fun"]}

Step 4: While iterating through the dictionary values I want to check and make sure values are not empty and not None.

When this process happens, I want to create a dictionary. For this dictionary,

KEY: indexes of step 2 (each index of each dictionary in the big list).

VALUES: a dictionary that has key from the step 3's keys (from my dictionary above) and values as (tricky part) a list, if the step 3 dictionary's value is not empty. As you can see below, I have an empty list temporary_keyword_list that should save the non empty lists values into a temporary list, but I am not getting what I want.

Below is what I tried, what I get and what my desired output.

output_1 = {}
for index, each_keyword in enumerate(sample_tuple):

    for ind, each_file in enumerate(each_keyword):
        temporary_dict = {}

        for key, value in each_file.items():

            temporary_keyword_list = []
            # Check if a value is not empty or not None
            if value!= [] and value is not None:
                temporary_keyword_list.append(index) ## Here I want to save the index (tricky part)

            # Start inserting values into the dictionary. 
            temporary_dict[key] = temporary_keyword_list

        # Final big dictionary 
        output_1[ind] = temporary_dict  

My current output_1 dictionary:

{0: {1: [1], 2: [], 3: [1]}, 1: {1: [], 2: [1]}}

Desired output:

{0: {1: [0, 1], 2: [0], 3: [0, 1]}, 1: {1: [0], 2: [0, 1]}}

Since its tuples, lists and dictionaries I tried my best to explain the problem I have. Please let me know in the comment if this doesn't make sense, I'll try my best to explain. Any help or suggestion would be awesome.

Upvotes: 1

Views: 556

Answers (1)

Patol75
Patol75

Reputation: 4547

You probably do not need to create temporary lists or dictionaries here as you can obtain all the indices you need from your for loops. The key here is that your initial tuple contains lists which have a similar structure, so in your code, the structure of your final dictionary is already determined after the first iteration of the first for loop. Consider using defaultdict as well when you create the inner dictionaries as you plan to store lists inside them. Then it is all about correctly handling indices and values. The code below should work.

from collections import defaultdict
sample_tuple = ([{1: ["hello"], 2: ["this"], 3: ["is fun"]},
                 {1: ["hi"], 2: ["how are you"]}],
                [{1: ["this"], 2: [], 3: ["that"]}, {1: [], 2: ["yes"]}])
output_1 = {}
for index, each_keyword in enumerate(sample_tuple):
    for ind, each_file in enumerate(each_keyword):
        if index == 0:
            output_1[ind] = defaultdict(list)
        for key, value in each_file.items():
            if value != [] and value is not None:
                output_1[ind][key].append(index)
            print(output_1)

To answer your comment, you can manage without defaultdict, but do you really want to do that ?

sample_tuple = ([{1: ["hello"], 2: ["this"], 3: ["is fun"]},
                 {1: ["hi"], 2: ["how are you"]}],
                [{1: ["this"], 2: [], 3: ["that"]}, {1: [], 2: ["yes"]}])
output_1 = {}
for index, each_keyword in enumerate(sample_tuple):
    for ind, each_file in enumerate(each_keyword):
        for key, value in each_file.items():
            if index == 0:
                if key == 1:
                    if value != [] and value is not None:
                        output_1[ind] = {key: [index]}
                else:
                    if value != [] and value is not None:
                        output_1[ind][key] = [index]
            else:
                if value != [] and value is not None:
                    output_1[ind][key].append(index)
            print(output_1)

Upvotes: 1

Related Questions