Python: searching for tuples nested in a list | Sololearn: Learn to code for FREE!
Neuer Kurs! Jeder Programmierer sollte generative KI lernen!
Kostenlose Lektion ausprobieren
0

Python: searching for tuples nested in a list

Hi, I am working through the Intermediate Python lesson. In Practice 4.2 I have to write code that takes input and searches for a string contained in a tuple that is nested in a list and output the second item in the tuple. Like this: List = [ (“Joe”, 42), (“Harry”, 30), (“Amanda”, 63)] Input: “Joe” Output: 42 I don’t know how to construct this search and reference the second index in the tuple. What is an efficient way to do this? Thanks!

19th May 2021, 2:41 PM
Ellison
Ellison - avatar
20 Antworten
+ 1
#Finally a solution! contacts = [ ('James', 42), ('Amy', 24), ('John', 31), ('Amanda', 63), ('Bob', 18) ] nam = input() for item in contacts: if nam == item[0]: print(nam + " is " + str(item[1])) break else: continue print("Not found")
19th May 2021, 9:07 PM
Ellison
Ellison - avatar
+ 4
Have you tried it??
19th May 2021, 2:46 PM
sarada lakshmi
sarada lakshmi - avatar
+ 4
I hope this can be helpful: contacts = [ ('James', 42), ('Amy', 24), ('John', 31), ('Amanda', 63), ('Bob', 18) ] out='Not Found' name=input() for x in contacts: if name==x[0]: age =x[1] out =x[0]+" is "+str(x[1]) break print(out)
16th Sep 2022, 8:46 AM
Alireza Shahrabi
Alireza Shahrabi - avatar
+ 2
Maybe it's a little cheat, but but nothing worked for me, neither the answer sololearn wrote letter by letter, nor the answers here from the forum... I convert into dictonary and it should be paste EXCATLY that way with ALL INDENTATIONS : contacts = [ ('James', 42), ('Amy', 24), ('John', 31), ('Amanda', 63), ('Bob', 18) ] contacts = dict(contacts) name = input() for i in contacts: if name == i : print(i + " is " + str(contacts[i])) break else: print("Not Found") I would appreciate if you could let me know if it works for you :))
11th Nov 2022, 2:23 PM
Coffee
+ 1
Ellison If you want to access a specific element inside the tuple which is inside the list, then you should have two close square brackets. Because it is a tuple inside the list. So first get the tuple 🔸contacts[i] And then get the first element of that tuple. 🔸contacts[i][0] Therefore the condition on your of statement should be: _________________________ while i < len(contacts): if contacts[i][0] == nam: # get the age print(contacts[i][1] _________________________
19th May 2021, 3:22 PM
noteve
noteve - avatar
+ 1
Awesome! Thank you. Would it be better to use a ‘for’ statement instead?
19th May 2021, 3:24 PM
Ellison
Ellison - avatar
+ 1
Ellison Yes, it would also be shorter. Using for loop, you can just iterate each tuple of the list then do the same as you did. It is like the while loop, but the while loop has incremention and is more used for conditions. That's why I also think for loop will be better for this particular problem. Thanks for bringing that up. __________________ for tup in contacts: if tup[0] == nam: print(tup[1]) __________________
19th May 2021, 3:27 PM
noteve
noteve - avatar
+ 1
Eve so here is where I am: contacts = [ ('James', 42), ('Amy', 24), ('John', 31), ('Amanda', 63), ('Bob', 18) ] nam = input() for item in contacts: for y in item: if y[0] == nam: print(nam + " is " + str(y[1])) else: print("Not found")
19th May 2021, 6:35 PM
Ellison
Ellison - avatar
+ 1
The error: for y[0] == nam TypeError: ‘int’ object is not scriptable.
19th May 2021, 6:38 PM
Ellison
Ellison - avatar
+ 1
Ellison, Eve and others - NONE of your solutions work. So whatever you mean by 'finally a solution'...... Ali - your code looks almost the same as mine & of course it doesn't work just like yours. And I can't say why because as a non-pro user, I don't see test case #5 that doesn't go through. FYI: instead of concatenating within print statement, you can f-string items: print ( f ' {name_found} is {tuple_name [1] } ' ) ----> spaces only for clarity.
24th Aug 2022, 1:37 PM
Monika
+ 1
Just like Coffee I transformed the list to a dictionary since it is easier to get the values individually that way though I didn't use a loop. The code; contacts = [ ('James', 42), ('Amy', 24), ('John', 31), ('Amanda', 63), ('Bob', 18) ] ncontacts = dict(contacts) # Making the nested tuple a dictionary name = input() # Taking input in name var # creating the search(i.e: running the input through the dictionary) def search(): if name in ncontacts: print(str(name) + ' is ' + str(ncontacts.get(name))) else: print('Not Found') search() #Search Function Declaration Hope it helps
30th Jan 2023, 10:07 AM
Vincent Ikem
Vincent Ikem - avatar
+ 1
Ellison i have made adjustment And this can be your good to go solution contacts = [ ('James', 42), ('Amy', 24), ('John', 31), ('Amanda', 63), ('Bob', 18) ] name = input() for item in contacts: if name == item[0]: print(name,"is",str(item[1])) break else: continue else: print("Not Found")
19th Sep 2023, 6:41 AM
Navin kumar
Navin kumar - avatar
0
Yes. Here is what I tried: contacts = [ ('James', 42), ('Amy', 24), ('John', 31), ('Amanda', 63), ('Bob', 18) ] nam = input() i = 0 while i < len(contacts): if contacts[i, 0]== nam: print(nam + " is " + contacts[i, 1]) i += 1 else: print("Not found") i += 1
19th May 2021, 3:08 PM
Ellison
Ellison - avatar
0
contacts = [ ('James', 42), ('Amy', 24), ('John', 31), ('Amanda', 63), ('Bob', 18) ] x = input () for i in contacts : if x == i[0] : print(str(i[0])+" is " +str(i[1])) break else: continue print ("Not Found") What is the problem of above code ? It does'nt output " Not found"
11th Jul 2022, 10:19 AM
Ali Teimori
Ali Teimori - avatar
0
The first solution offered by Ellison works for cases 1 & 2 but doesn't proceed to the rest of the tests.
8th Sep 2022, 3:07 PM
Dixie Dixie
Dixie Dixie - avatar
0
I studied the codes shared here and I've got a few ideas. This is what worked for me in the tuple exercise: contacts = [ ("James", 42), ("Amy", 24), ("John", 31), ("Amanda", 63), ("Bob", 18) ] contacts_dict = dict(contacts) name = input() def name_found(name): for name_contact in contacts_dict: if name == name_contact: print(name + " is " + str(contacts_dict[name_contact])) else: continue if name in contacts_dict: name_found(name) else: print("Not Found") Sorry If I have some typos, hopefully you get the idea of what I'm trying to do here. Good luck.
15th Aug 2023, 12:49 AM
Carlos Valdez
0
#Finally I looked through a lot of these responses. Definitely feel like the lessons previously didn't teach any of this. here is the simplified code I used trying to only use things covered in the lessons. #list contacts = [ ('James', 42), ('Amy', 24), ('John', 31), ('Amanda', 63), ('Bob', 18) ] #create dictionary contacts_dict = dict(contacts) #define name name = (input()) #search for name and print if name in contacts_dict: print (name, "is", str(contacts_dict[name])) else: print ("Not Found")
28th Aug 2023, 6:38 PM
James Burier
James Burier - avatar
0
#It was hard as a beginner, but this works. contacts = [ ('James', 42), ('Amy', 24), ('John', 31), ('Amanda', 63), ('Bob', 18) ] name = input() for x in contacts: if name == str(x[0]): print(str(x[0]),"is",int(x[1])) break elif name != str(x[0]): continue if name != str(x[0]): print("Not Found")
23rd Sep 2023, 6:16 PM
Kasun Soysa
Kasun Soysa - avatar
0
contacts = [ ('James', 42), ('Amy', 24), ('John', 31), ('Amanda', 63), ('Bob', 18) ] name = input() result = "Not Found" for x in contacts: if name == x[0]: result = (x[0] + " is " + str(x[1])) print(result)
12th Oct 2023, 12:36 AM
Anton Chystiakov
0
contacts = [ ('James', 42), ('Amy', 24), ('John', 31), ('Amanda', 63), ('Bob', 18) ] nam = input() found = False for item in contacts: if nam == item[0]: print(nam + " is " + str(item[1])) found = True break if not found: print("Not Found")
23rd Nov 2023, 5:12 PM
Mohamad Azhar Azman
Mohamad Azhar Azman - avatar