print jsonfile key that it's value is selected by input
<p>I have following codes and problems , Any idea would help. Thanks...</p>

<p><strong>Nouns.json :</strong></p>

<pre><code>{
"hello":["hi","hello"],
"beautiful":["pretty","lovely","handsome","attractive","gorgeous","beautiful"],
"brave":["fearless","daring","valiant","valorous","brave"],
"big":["huge","large","big"]
}
</code></pre>

<p><strong>Python file :</strong> this code would find word synonyms from json file and prints them</p>

<pre><code>import random
import json

def allnouns(xinput):
    data = json.load(open('Nouns.json'))
    h = ''
    items = []
    T = False
    for k in data.keys():
        if k == xinput:
            T = True
    if T == True:
        for item in data[xinput]:
            items.append(item)
        h = random.choice(items)
    else:
        for k in data.keys():
            d = list(data.values())
            ost = " ".join(' '.join(x) for x in d)
            if xinput in ost:
                j = ost.split()
                for item in j:
                    if item == xinput :
                        h = k
                        break
                    else:
                        pass
            else :
                h = xinput

    print(h)
xinput = input(' input : ')
allnouns(xinput)
</code></pre>

<p><strong>Example:</strong></p>

<pre><code>example for input = one of keys :

&gt;&gt; xinput = 'hello' &gt;&gt; hello
&gt;&gt; 'hi' or 'hello' &gt;&gt; hi

example for input = one of values :

&gt;&gt; xinput = 'pretty' &gt;&gt; pretty
&gt;&gt; it should print it's key (beautiful) but it prints the first key (hello) &gt;&gt; hello
</code></pre>

<p>Problem is the last line of example</p>

<p>Any ideas how to fix it?</p>

<p>This looks massively over-complicated. Why not do something like:</p>

<pre><code>import json

def allnouns(xinput):
    nouns = json.load(open('Nouns.json'))
    for key, synonyms in nouns.items():
        if xinput in synonyms:
            print(key)
            return;

xinput = input(' input : ')
allnouns(xinput)
</code></pre>

python json python-3.x