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

Python

Trying to remove inside of a for loop ".remove(i)", it does not remove every match, only the first match.

I'm trying to remove every match inside of the list if given a specific name.

if you test this bellow, it will only remove one of the students in the list. try it putting "jose" in the imput, it will only remove the first "jose".

THANKS IN ADVANCE FOR THE HELP

y = 'c'

student_list = [('jose', 20, 20),('jose', 20, 20),('jose', 20, 20), ('cristina', 20, 20)]

if y == 'c': for i in student_list: student_name = input('Enter the name: '.lower()) if student_name == i[0]: student_list.remove(i) print (student_list)

1 Answer

Sorry, i have found the answer:

y = 'c'

student_list = [('jose', 20, 20),('jose', 20, 20),('jose', 20, 20), ('cristina', 20, 20)]

if y == 'c': for i in student_list: #'jose', 20, 20' student_name = input('Enter the name: '.lower()) #'jose' if student_name == i[0]: while i in student_list: student_list.remove(i) print (student_list)