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

Aaron Peter
Aaron Peter
1,498 Points

How to delete information from a list with a nested tuples ?

student_list = [] y = 0 Exammark = [] while y!='x':
#print('a. Add new student records') #print('b. Show all student records') #print('c. Delete a student record') #print('d. Display overall average coursework mark') #print('e. Display overall average exam mark') #print('f. Display all aggregate marks') #print('g. Display overall average aggregate mark') y = input("Enter a Letter: ")

if y == 'a':
    while True:
        student_name =str(input("Enter your name: "))
        coursework =int(input("Enter your coursework mark: "))
        if coursework <0 or coursework > 100 :
            print ( " The value is out of range try again")

        else:
            break
    while True:
        exammark =int(input("Enter your results: "))
        if exammark <0 or exammark > 100 :
            print ( " The value is out of range try again")
        else:
            break
    student_list.append((student_name, coursework, exammark),)


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

the last section is me trying to remove the student from the list , it doesn't work

1 Answer

Steven Parker
Steven Parker
231,269 Points

Since the items in the list are all 3-piece tuples, comparing them to just the name will never produce a match. Since "i" represents the entire tuple, you need to index into it to make the comparison, and use it as the argument to "remove":

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

Note that the input was moved and is now done before the loop starts

Aaron Peter
Aaron Peter
1,498 Points

Thank you so much !