Help to solve TLE in https://www.codechef.com/problems/ATTND/

from sys import stdin
from sys import stdout

fname=[]
lname=[]
t=int(stdin.readline())
while(t>0):
n=int(stdin.readline())
for i in range(n):
a,b=input().split(" ")
fname.append(a)
lname.append(b)

for j in range (0,n) :
    if(fname.count(fname[j]))>=2:
        stdout.write(str(fname[j])+" "+str(lname[j])+  '\n')
    else:
        stdout.write(str(fname[j])+ '\n')

t-=1

Hello. There is a problem with fname and lname lists.

You know what? It works fine for first test case. But starting from 2nd testcase, it still has old data. So there are 2 ways to fix this.

First way is to transfer those 2 lines down inside while loop so that for each iteration of while loop you get clean new list with no data. You can see my submission. Link

Second way is if to use clear function on lists on each iteration of while loop. Link

Please Upvote :+1: if it helped. It motivates me to write more answers.

1 Like

Thanks for the reply.
It worked!