I am currently trying to write code for iterating through a list of tuples and if the names match check who is older and remove the younger one from the list. I am running into an issue where if I remove a tuple, the code still tries to iterate through the original range even though I update the variable l. Code:
dads = [('Jim', '30'), ('Jeff', '45'), ('Jim', '35')]
l = len(dads)
for i in range(l):
for j in range(l):
if dads[i][0] == dads[j][0]:
if int(dads[i][1]) > int(dads[j][1]):
dads.pop(j)
l = len(dads)
if j < i:
j -= 1
i -= 1
print(dads)