+ 2

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

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

13th Feb 2022, 11:09 AM
Edward Marais
Edward Marais - avatar
10 Answers
+ 4
def find(search_name, data): for idx, (name, age) in enumerate(data): if name == search_name: return idx There are multiple tricks here - enumerate function associates the index position with each data element - "for i, d in enumerate..." The index and the actual data is assigned to two different variables - in this case each piece of data is a tuple of two elements, so it can further be destructured to variables: "for idx, (name, age)" - in addition to using the search name as argument, I am passing the contacts also. This is a principle of functional programming that a functions should not have any external dependencies apart from its arguments. https://code.sololearn.com/cRSvvu609s2R/?ref=app
13th Feb 2022, 11:30 AM
Tibor Santa
Tibor Santa - avatar
+ 4
Even without knowing all the functions, you can achieve the result you need. You just need to use a loop to iterate over the index of the list and correct your mistakes of course: name = input()or"James" loop☺️: if contacts[i][0] == name: age = contacts[i][1] print(name +" is "+ str(age))
13th Feb 2022, 11:57 AM
Solo
Solo - avatar
+ 4
Simba, you forgot to change grade to j, but it's better to fix both on age ☺️
13th Feb 2022, 1:39 PM
Solo
Solo - avatar
+ 3
Why don't you use a dictionary? Your data seems to have been made for that.
13th Feb 2022, 11:34 AM
Paul
Paul - avatar
+ 3
Paul, in your case the condition is no longer needed: contacts = dict(contacts) print(name, "is", contacts.get(name))
13th Feb 2022, 1:09 PM
Solo
Solo - avatar
+ 2
You can also do it like this name = input() for i,j in contacts: if i == name: print(i ,"in",j) break else: print("Not found")
13th Feb 2022, 1:27 PM
Simba
Simba - avatar
+ 1
#Your way : contacts= [ ("James",42), ("Amy",24), ("John",31), ("Amanda",63), ("Bob",18) ] name = input() for i in range(len(contacts)) : if contacts[i][0] == name: age = contacts[i][1] print(f"At index {i} : {name} is {age}")
13th Feb 2022, 11:33 AM
Jayakrishna 🇮🇳
0
Paul can you show me
13th Feb 2022, 11:41 AM
Edward Marais
Edward Marais - avatar
0
Something like this, it doesn't work with id's but i don't understand why you want that anyway: contacts= [ ("James",42), ("Amy",24), ("John",31), ("Amanda",63), ("Bob",18) ] name = input() contacts = dict(contacts) if name in contacts: print(name + " is " + str(contacts[name])) else: print(name + " not found")
13th Feb 2022, 12:50 PM
Paul
Paul - avatar
0
#And @solo further, not to return none for input : contacts = { "James" : 42, "Amy" : 24, "John" : 31, "Amanda": 63, "Bob" : 10 } name = input() print(name + " is " + str( contacts.get( name, "not found" )))
13th Feb 2022, 1:16 PM
Jayakrishna 🇮🇳