Select element in one list based on the position of n item in another list
Question:
title = ['james','tom','kim']
post_id = [5,6,9]
what_to_look = input('Enter Name: ')
x = title == post_id
if what_to_look == x:
print('find')
else:
print('not found')
Hi guys how to get the id 6 in this code? what is wrong why is not working?
Answers:
To get post_id
by the respective title you can use .index()
method. You can follow the algorithm:
- Find if
What_to_look
is in your title
list
- if yes, get the index of it using
.index()
method.
- get the element at the same position of
post_id
list.
so Your final code should be:
title = ['james','tom','kim']
post_ids = [5,6,9]
what_to_look = input('Enter Name: ')
if what_to_look in title:
position = title.index(what_to_look)
post_id = post_ids[position]
print('found', post_id)
else:
print('not found')
Now you can get id 6 by entering ‘tom’
You can use zip
function as follows:
title = ['james','tom','kim']
post_id = [5,6,9]
title_id = list(zip(title, post_id))
what_to_look = input('Enter Name: ')
for i in title_id:
if i[0] == what_to_look:
print(f"found. id = {i[1]}")
break
else: # it is for-else not if-else condition
print("not found")
input: james
output: found. id = 5
(Notice that using a dictionary will be much better and more efficient)
Given two lists of equal length you can use zip(). (Actually the lists don’t have to be the same length but let’s keep it simple)
title = ['james','tom','Kim']
post_id = [5,6,9]
query = input('Enter name: ')
for name, pid in zip(title, post_id):
if name == query:
print(pid)
break
else:
print('Not found')
Alternatively, convert your lists into a single dictionary then you can do a direct lookup as follows:
title = ['james','tom','Kim']
post_id = [5,6,9]
query = input('Enter name: ')
print(dict(zip(title, post_id)).get(query, 'Not found'))
title = ['james','tom','kim']
post_id = [5,6,9]
what_to_look = input('Enter Name: ')
x = title == post_id
if what_to_look == x:
print('find')
else:
print('not found')
Hi guys how to get the id 6 in this code? what is wrong why is not working?
To get post_id
by the respective title you can use .index()
method. You can follow the algorithm:
- Find if
What_to_look
is in yourtitle
list - if yes, get the index of it using
.index()
method. - get the element at the same position of
post_id
list.
so Your final code should be:
title = ['james','tom','kim']
post_ids = [5,6,9]
what_to_look = input('Enter Name: ')
if what_to_look in title:
position = title.index(what_to_look)
post_id = post_ids[position]
print('found', post_id)
else:
print('not found')
Now you can get id 6 by entering ‘tom’
You can use zip
function as follows:
title = ['james','tom','kim']
post_id = [5,6,9]
title_id = list(zip(title, post_id))
what_to_look = input('Enter Name: ')
for i in title_id:
if i[0] == what_to_look:
print(f"found. id = {i[1]}")
break
else: # it is for-else not if-else condition
print("not found")
input: james
output: found. id = 5
(Notice that using a dictionary will be much better and more efficient)
Given two lists of equal length you can use zip(). (Actually the lists don’t have to be the same length but let’s keep it simple)
title = ['james','tom','Kim']
post_id = [5,6,9]
query = input('Enter name: ')
for name, pid in zip(title, post_id):
if name == query:
print(pid)
break
else:
print('Not found')
Alternatively, convert your lists into a single dictionary then you can do a direct lookup as follows:
title = ['james','tom','Kim']
post_id = [5,6,9]
query = input('Enter name: ')
print(dict(zip(title, post_id)).get(query, 'Not found'))