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>What you are trying to do is called a "lookup". Given a key (u'88', or other rating), you want a value (the dictionary where 'rating' is u'88'). A list object, like you currently have the dictionaries stored in, is not very good for such lookups, because you have to go through each index in the list, compare the rating to the key, and then return the dictionary (if it exists at all).</p>

<pre><code>key = u'88'
for d in Data:
    if d['rating'] == key:
        print d
</code></pre>

<p>Consider what happens when you have a large amount of dictionaries stored in that list. What if none of the dictionaries have a rating equal to the key? You pass through a large number of dictionaries, checking for equalities, only to have none exist. This is wasted computations.</p>

<p>As you know, python's <code>dict</code> type is very-well suited for lookups. If you had a dictionary that looked like</p>

<pre><code>Data_dict = {u'88': {u'rare': u'1', u'name': u'Pogba', ...},
             u'89': {u'rare': u'1', u'name': u'Pogba', ...} }
</code></pre>

<p>then you could quickly see if the key u'88' is in that dataset.</p>

<pre><code>if u'88' in Data_dict:
    print Data_dict[u'88']
</code></pre>

<p>To make such a dictionary, from your current Data as a list:</p>

<pre><code>Data_dict = {val[u'rating']: val for val in Data}
</code></pre>

python json python-2.7 dictionary