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

How can i achieve like this in python

title = ['james','tom','Kim'] # imagine this is a a file.txt
post_id = [5,6,9] #imagine this is a a file.txt

query = ['james','Kim'] # imagine this is a a file.txt

for i in query:
    print(i)

for name, pid in zip(title, post_id):
    if name == i:
        print(pid)
        break
else:
    print('Not found')

Out Put

james
Kim
9

what im trying to do is i want to make a file.txt listed all post together with their post ID but i have a few item i want to check for a list of specific posts.
How to achieve like this bro?

What I’m Expected 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

tom = 6
kim = 9

>Solution :

You can remove

for i in query:
      print(i)

And remove zip(query, post_id): and use for name in query: to do comparing. By using this your code will loop through every value of query and will compare it with title.

So your final code should be:

title = ['james','tom','Kim'] 
post_id = [5,6,9] 

query = ['james','Kim'] 

for name in query:
   ids = post_id[title.index(name)]
   if name in title:
      print(f"{name} = {ids}")
    
   else:
      print('Not found')
  
  
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