Python

Aaron Peter
Aaron Peter
1,004 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
98,202 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,004 Points

Thank you so much !