+ 1

How to get the index posisition of name in a list tuple?

contacts= [ ("James",42), ("Amy",24), ("John",31), ("Amanda",63), ("Bob",18) ] #I want the user to give me a name, and then i want to check if that name is in the tuple and it should return the index posistion to a variable of the name in the tuple #Also the index posistion of the name age in the tuple #what i did was name = input(#James) if contacts [0][0] == name: age = contacts[0][1] print(name +" is "+ str(age)) #another problem is i don't know how to create a function that would create these indexs to find if the word is there or not if anyone could just help me, with getting the index posistion inside of the tuple

17th Jul 2021, 10:18 AM
Edward Marais
Edward Marais - avatar
3 odpowiedzi
17th Jul 2021, 10:42 PM
David Carroll
David Carroll - avatar
+ 6
a bit late, but anyway it could be interesting: (it uses a list comprehension with an integrated print() that uses an f-string) contacts= [("James",42),("Amy",24),("John",31),("Amanda",63),("Bob",18)] name = input() [print(f"{i[0]} is {i[1]}") for i in contacts if name in i]
2nd Aug 2021, 7:44 PM
Lothar
Lothar - avatar
+ 2
Thank you so much for the response i will try that
17th Jul 2021, 10:29 AM
Edward Marais
Edward Marais - avatar