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

way to compare between index of two lists in python

list1=['name1','name2','name3']
list2=[123,1234,12345]

i wanna say in python if index of ‘name1’in the first list is equal to index of 123 in the second list without knowing them for example ‘name1’ and 123 is user input

i have tried many things like using .index but i get an error TypeError: list indices must be integers or slices, not str
TypeError: list indices must be integers or slices, not str

and tried many other things but none worked as i wanted

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

>Solution :

You can do the following:

list1 = ['name1', 'name2', 'name3']
list2 = [123, 1234, 12345]

name = input("name: ")
num = int(input("num: "))  # don't forget to convert to int

if (name, num) in zip(list1, list2):
    print("Yay")
else:
    print("Aww")
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