How can I get values from dictionary for particular key using python
<p>In this code, I wanted to identify a member with his ID. If the ID exist in the dictionary, I would like to print the fullname of this member</p>

<pre><code>iin = input('Enter your ID :')
 dict = {'id' : ['aa', 'bb', 'cc'],
        'Firstname' : ['Mark', 'Jamal', 'Van'],
        'Lastname' : ['Roch', 'borh', 'nilsa']}
 for d in dict:
    if inn in d:
       print('Hello,', dict[inn])
    else :
       print('Sorry, you are not a member')
</code></pre>

<p>The desired result</p>

<pre><code>Enter your ID : aa
Hello, Mark Roch
</code></pre>

<p>Thank you for the help</p>

<p>The working version of the code should be:</p>

<pre><code>my_dict = {'id' : ['aa', 'bb', 'cc'],
           'Firstname' : ['Mark', 'Jamal', 'Van'],
               'Lastname' : ['Roch', 'borh', 'nilsa']}
iin = input('Enter your ID :')
# Enter your ID :"bb"
try:
    my_id = my_dict['id'].index(iin)
    print my_dict['Firstname'][my_id], ', ', my_dict['Lastname'][my_id]
except ValueError:
    print 'Sorry, you are not a member'    
# Jamal ,  borh
</code></pre>

<p><strong>Exlaination/Issues with your code:</strong></p>

<ol>
<li><code>for d in dict</code> means <code>for d in dict.keys()</code> which will return <code>['id', 'FirstName', 'LastName'</code>. In order to iterate over id, you should be doing <code>for d in dict['id']</code>. In fact, iterating over the list itself is not required. To get <code>index</code> of element in ;list, you may simply use <code>list.index(element)</code> function</li>
<li><code>dict</code> is the <code>built-in</code> class in <code>python</code>. You should  never use the keywords</li>
<li><p>You dictionary structure is itself not correct. By definition, <code>dict means collections of objects</code> and object means similar entities. In this case, Person. Since id is supposed by unique, in am making nested dict with key as <code>id</code> (you may also use list of dict):</p>

<pre><code>{'aa': {'Firstname' : 'Mark',
        'Lastname' : 'Roch'},
 'bb': {'Firstname' : 'Jamal',
        'Lastname' : 'borh'},
 'cc': {'Firstname' : 'Van',
        'Lastname' : 'nilsa'},
}
</code></pre></li>
</ol>

python for-loop dictionary input output