Replace userinput list elements from master dictionary?
<p>Here, i have a master dic whose value consist of list of list.</p>

<p>Whenever user inputs the value as a list of list, i want my program to replace those user input list from the master list.</p>

<p>I have tried and it is giving me expected output but it consists of lot of temporary lists which is annoying , Is there any other pythonic way to achieve the same without using unwanted temporary list ?</p>

<p>Input dic :</p>

<pre><code>master_dic = {
    'First': [[{'A': '#d8', 'B': '#d7'}, {'C': '#d8', 'D': '#d7'}], [{'A': '#d8', 'B': '#d7'}, {'C': '#d8', 'D': '#d7'}],
              [{'A': '#d8', 'B': '#d7'}, {'C': '#d8', 'D': '#d7'}], [{'A': '#d8', 'B': '#d7'}, {'C': '#d8', 'D': '#d7'}]],
    'Second': [[{'A': '#d8', 'B': '#d7'}, {'C': '#d8', 'D': '#d7'}], [{'A': '#d8', 'B': '#d7'}, {'C': '#d8', 'D': '#d7'}],
               [{'A': '#d8', 'B': '#d7'}, {'C': '#d8', 'D': '#d7'}], [{'A': '#d8', 'B': '#d7'}, {'C': '#d8', 'D': '#d7'}]],
    'Third': [[{'A': '#d8', 'B': '#d7'}, {'C': '#d8', 'D': '#d7'}], [{'A': '#d8', 'B': '#d7'}, {'C': '#d8', 'D': '#d7'}],
              [{'A': '#d8', 'B': '#d7'}, {'C': '#d8', 'D': '#d7'}], [{'A': '#d8', 'B': '#d7'}, {'C': '#d8', 'D': '#d7'}]],
    'Forth': [[{'A': '#d8', 'B': '#d7'}, {'C': '#d8', 'D': '#d7'}], [{'A': '#d8', 'B': '#d7'}, {'C': '#d8', 'D': '#d7'}],
              [{'A': '#d8', 'B': '#d7'}, {'C': '#d8', 'D': '#d7'}], [{'A': '#d8', 'B': '#d7'}, {'C': '#d8', 'D': '#d7'}]]
}
</code></pre>

<p>My Code : </p>

<pre><code>userinput = [[['First'], ['Forth']], [['Second'], ['First']], ['Third'], ['Forth']]

outer = []
for i in userinput:
    new = []
    if len(i) &gt; 1:
        temp = []
        for j in i:
            jj = master_dic[j[0]]
            temp.append(jj)
        new.append(temp)
        outer.append(new)
    else:
        for k in i:
            kk = master_dic[k]
            new.append(kk)
            outer.append(new)
data_join = [i[0] for i in outer]
</code></pre>

<p>Output :</p>

<pre><code>[
    [[[{'A': '#d8', 'B': '#d7'}, {'D': '#d7', 'C': '#d8'}], [{'A': '#d8', 'B': '#d7'}, {'D': '#d7', 'C': '#d8'}], [{'A': '#d8', 'B': '#d7'}, {'D': '#d7', 'C': '#d8'}], [{'A': '#d8', 'B': '#d7'}, {'D': '#d7', 'C': '#d8'}]], [[{'A': '#d8', 'B': '#d7'}, {'D': '#d7', 'C': '#d8'}], [{'A': '#d8', 'B': '#d7'}, {'D': '#d7', 'C': '#d8'}], [{'A': '#d8', 'B': '#d7'}, {'D': '#d7', 'C': '#d8'}], [{'A': '#d8', 'B': '#d7'}, {'D': '#d7', 'C': '#d8'}]]],

    [[[{'A': '#d8', 'B': '#d7'}, {'D': '#d7', 'C': '#d8'}], [{'A': '#d8', 'B': '#d7'}, {'D': '#d7', 'C': '#d8'}], [{'A': '#d8', 'B': '#d7'}, {'D': '#d7', 'C': '#d8'}], [{'A': '#d8', 'B': '#d7'}, {'D': '#d7', 'C': '#d8'}]], [[{'A': '#d8', 'B': '#d7'}, {'D': '#d7', 'C': '#d8'}], [{'A': '#d8', 'B': '#d7'}, {'D': '#d7', 'C': '#d8'}], [{'A': '#d8', 'B': '#d7'}, {'D': '#d7', 'C': '#d8'}], [{'A': '#d8', 'B': '#d7'}, {'D': '#d7', 'C': '#d8'}]]],

    [[{'A': '#d8', 'B': '#d7'}, {'D': '#d7', 'C': '#d8'}], [{'A': '#d8', 'B': '#d7'}, {'D': '#d7', 'C': '#d8'}], [{'A': '#d8', 'B': '#d7'}, {'D': '#d7', 'C': '#d8'}], [{'A': '#d8', 'B': '#d7'}, {'D': '#d7', 'C': '#d8'}]],

    [[{'A': '#d8', 'B': '#d7'}, {'D': '#d7', 'C': '#d8'}], [{'A': '#d8', 'B': '#d7'}, {'D': '#d7', 'C': '#d8'}], [{'A': '#d8', 'B': '#d7'}, {'D': '#d7', 'C': '#d8'}], [{'A': '#d8', 'B': '#d7'}, {'D': '#d7', 'C': '#d8'}]]
]
</code></pre>

<p>No of list to be maintained </p>

<pre><code>[
    [[[ {First value} ]], [[ {Forth value} ]]],

    [[[ {Second value} ]],[[ {First value} ]]],

    [[ {Third value} ]],

    [[ {Forth value} ]]
]
</code></pre>

<p>I think this is what you want</p>

<pre><code>def array_replace(userinput,master_dic):
    for i,data in enumerate(userinput):
        if isinstance(data, list):
            found=array_replace(data,master_dic)
            if not isinstance(found, list):
                userinput[i]=master_dic[found]
        else:
            return data
    return userinput
</code></pre>

<p><strong><a href="https://repl.it/DUks" rel="nofollow">DEMO</a></strong></p>

<p>Hope this helps</p>

python list dictionary