+ 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
3 odpowiedzi
+ 6
This might also be helpful.
https://code.sololearn.com/ccEYBI3X5I7r/?ref=app
+ 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]
+ 2
Thank you so much for the response i will try that