Extracting information depending on user input from a dictionary
<p>I have the following information from a <a href="http://www.futbin.com/api?term=paul%20pogba" rel="nofollow">.json</a> loaded and stored in a variable <code>Data</code>:</p>

<pre><code>Data = [
    {u'rating': u'89', u'rare': u'1', u'name': u'Pogba', u'club_image': u'/content/fifa17/img/clubs/11.png', u'image': u'/content/fifa17/img/players/195864.png', u'rare_type': u'3', u'version': u'IF', u'full_name': u'Pogba', u'position': u'CDM', u'id': u'15073', u'nation_image': u'/content/fifa17/img/nation/18.png'},
    {u'rating': u'89', u'rare': u'1', u'name': u'Pogba', u'club_image': u'/content/fifa17/img/clubs/11.png', u'image': u'/content/fifa17/img/players/195864.png', u'rare_type': u'21', u'version': u'OTW', u'full_name': u'Pogba', u'position': u'CM', u'id': u'15091', u'nation_image': u'/content/fifa17/img/nation/18.png'},
    {u'rating': u'88', u'rare': u'1', u'name': u'Pogba', u'club_image': u'/content/fifa17/img/clubs/11.png', u'image': u'/content/fifa17/img/players/195864.png', u'rare_type': u'1', u'version': u'Normal', u'full_name': u'Pogba', u'position': u'CM', u'id': u'78', u'nation_image': u'/content/fifa17/img/nation/18.png'}
]
</code></pre>

<p>I'd like to extract the information depending on the <code>rating</code> of the player inputted by the user, only if <em>another rating exists</em>. <em>EDIT</em> : <em>By this, I mean that if only one line was present, i.e. 1 rating, there would be no need to carry out the process.</em></p>

<p>For example, if the user inputted: <code>88</code>, it would return/print:</p>

<pre><code>{u'rating': u'88', u'rare': u'1', u'name': u'Pogba', u'club_image': u'/content/fifa17/img/clubs/11.png', u'image': u'/content/fifa17/img/players/195864.png', u'rare_type': u'1', u'version': u'Normal', u'full_name': u'Pogba', u'position': u'CM', u'id': u'78', u'nation_image': u'/content/fifa17/img/nation/18.png'}
</code></pre>

<p>From my minimal knowledge, I am aware that I must use dictionaries, but am unaware of how to do so. However, I am currently in the following stage in my attempts:</p>

<pre><code>player_dict = {Data}
player_info = player_dict.get(user_input)
if player_info:
    for item in player_info:
        player_info = item
</code></pre>

<p>^ <em>This doesn't seem to work at all.</em></p>

<p>The other answers don't include your requirement that data should only be returned if the <code>rating</code> appears in <code>Data</code> once only.</p>

<pre><code># First iterate over Data and count how many times that rating appears
counter = {}
for item in Data:
    rating = item['rating']
    counter[rating] = counter.get(rating, 0) + 1

# Now build a dict that uses rating as a key and the whole dataset with  
# that rating as a value. Include only ratings that appeared once in Data
cleaned_dict = {item['rating']: item for item in Data if counter.get(item['rating']) == 1}

query = raw_input('Enter rating:')

if cleaned_dict.get(query):
    print "FOUND"
    print cleaned_dict.get(query)
else:
    print "Not found, no need to process further"
</code></pre>

<p>Explanation:<br>
In the first <code>for</code> loop, we build a dictionary of <code>{rating: frequency}</code>. The line <code>counter[rating] = counter.get(rating, 0) + 1</code> says that if the <code>rating</code> isn't in the dictionary, give that rating a default count of <code>0</code> and immediately add <code>1</code> to it. i.e. it appears once. If, however, later in the <code>for</code> loop we find that rating again, we get the value stored against that rating and add <code>1</code> to it. You could also use the <a href="https://docs.python.org/2/library/collections.html" rel="nofollow"><code>Counter</code></a> object for this but I've found that usually to be slower to <code>get()</code>.</p>

<p>In <code>cleaned_dict</code> we use a dictionary comprehension to build a new set of data only if that rating appeared once in <code>Data</code>. We store the whole raw data against a key of just the <code>rating</code>. This allows fast lookup on user input.</p>

<p>Finally, we take user input and look for the rating in our dictionary. If the rating is not a key in <code>cleaned_data</code> then we get <code>None</code>, which is falsey, so <code>if cleaned_dict.get(query):</code> is interpreted as <code>False</code>.</p>

<p>EDIT based on comments:</p>

<pre><code>cleaned_dict = {}
for item in Data:
    rating = item['rating']
    is_in_dict = cleaned_dict.get(rating)
    if is_in_dict:
        position = item['position']
        cleaned_dict[rating][position] = item
    else:
        sub_dict = {}
        sub_dict[item['position']] = item
        cleaned_dict[rating] = sub_dict

rating_input = raw_input('Enter rating:')
check_rating = cleaned_dict.get(rating_input)
if check_rating:
    position_input = raw_input('Enter position:')
    if check_rating.get(position_input):
        print check_rating.get(position_input)
    else:
        print "Record of rating, but not position"
else:
    print "No record of rating {}".format(rating_input)
</code></pre>

python json python-2.7 dictionary