Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial
Daniel Čupak
6,602 PointsHow to scan through a list with another list's items?
Ok, I have a list of names, emails, phone numbers and I need to scan through this list with emails to match them, in other words:
list = [[John Doe, example@dot.com, 555 543 697, ] , [John Smith, smith@dot.com, 555 645 654]]
match = [John Doe, Jacob Dunbart]
I dont even know how to group the first list together...I just know it could be done with a for loop somehow
How to scan through a list (or dictionary?) with another list's items to find the matching pairs and to print them out
Thank you for any help
4 Answers
Andreas cormack
Python Web Development Techdegree Graduate 33,011 Pointsmy_list = [['John Doe', 'example@dot.com', '555 543 697' ] , ['John Smith', 'smith@dot.com', '555 645 654']]
my_match= ['John Doe', 'Jacob Dunbart']
for info in my_list:
if info[0] in my_match:
# print out the phone number and email for each match
print("Phone Number : {} email: {}".format(info[2],info[1]))
Chris Freeman
Treehouse Moderator 68,468 Pointsdef get_record(key):
"""Return record containing key"""
for record in records:
if key in record:
return record
for key in match:
get_record(key)
Gunhoo Yoon
5,027 PointsI guess you can try something like this?
# records is just an alias of list since using list as variable name is a bad idea.
for record in records:
if record[0] in match:
print(record)
Although, you might want to consider using dictionary or actual database management system.
Daniel Čupak
6,602 PointsYou guys rock and are the best! Thank you all!
this code is the best (I have just added name) - it is doing what I wanted to do with it
my_list = [['John Doe', 'example@dot.com', '555 543 697' ] , ['John Smith', 'smith@dot.com', '555 645 654']]
my_match= ['John Doe', 'Jacob Dunbart']
for info in my_list:
if info[0] in my_match:
# print out the phone number and email for each match
print("Name : {} Phone Number : {} email: {}".format(info[0],info[2],info[1]))