0

what is wrong in this program unable to get output??

#i am getting an error msg "NameError: free variable 'b' referenced before assignment in #enclosing scope" help me how to resolve it def orginal(n): b=[i for i in n if i not in b] print(b) n=[9,3,9,8,2,8,3,2,5,8] orginal(n)

13th Mar 2021, 6:59 PM
mohana hemanth
mohana hemanth - avatar
5 Answers
+ 2
def orginal(n): b = [] [b.append(i) for i in n if i not in b] print ("The list after removing duplicates : " , str(b)) n=[9,3,9,8,2,8,3,2,5,8] orginal(n) #this works
14th Mar 2021, 2:34 PM
mohana hemanth
mohana hemanth - avatar
+ 1
you cannot use b in your conditional statement unless it has already been declared. just declare b first as an empty list. _____________________________ def orginal(n): b = [] b=[i for i in n if i not in b] print(b) n=[9,3,9,8,2,8,3,2,5,8] orginal(n)
13th Mar 2021, 8:25 PM
Ethan
Ethan - avatar
+ 1
@ethan gallup i tried what u gave but it is not removing duplicates
14th Mar 2021, 2:27 PM
mohana hemanth
mohana hemanth - avatar
0
what is b doing in that list comprehension? Have you assigned some value to it before this expression.
13th Mar 2021, 7:32 PM
Abhay
Abhay - avatar
0
nice! good work!
14th Mar 2021, 2:51 PM
Ethan
Ethan - avatar