Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

python: index of list1 equal to index of list2

I have 2 lists , they are the same length and in correct order, what i need(i think) is that index of list1 is equal to index of list2 and print the list2 content at that index.

this is what i have so far:

id = [10610, 12772, 10611, 13434, 13397, 13854]

name = ['sarah', 'john', 'mark', 'james', 'jack']

userid = int(input('enter user ID: '))
ind = id.index(userid)
 
if userid in id:
    print(ind)
else:
    print('wrong ID')

output:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

enter user ID: 13434
3

what i want it to do is, to print the content thats at the same index in "name" list, in this case its "james"

thanks

>Solution :

well what the comments said and change it like the following too :

    id = [1, 2, 3, 4, 5, 6]

    name = ['sarah', 'john', 'mark', 'james', 'jack']

    userid = int(input('enter user ID: '))

    if userid in id:
        ind = id.index(userid) #notice this statement is inside the if and not outside
        print(name[ind])
    else:
        print("wrong id")

if you try to use inden(..) before checking if the userid (the one they entered) is in the id , then you will get ValueError: <some-numer> is not in list

so its better to do it inside the if

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading