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!
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")
+ 4
Have you tried it??
+ 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)
+ 3
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 :))
+ 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]
_________________________
+ 1
Awesome! Thank you. Would it be better to use a ‘for’ statement instead?
+ 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])
__________________
+ 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")
+ 1
The error:
for y[0] == nam
TypeError: ‘int’ object is not scriptable.
+ 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.
+ 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
+ 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")
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
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"
0
The first solution offered by Ellison works for cases 1 & 2 but doesn't proceed to the rest of the tests.
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.
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")
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")
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)
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")