Trouble to create a list without duplicates
Hey guys,
Maybe I made a very naive mistake but when I try to create a list without duplicates It doesn't work and I can't figure it out why... While the same piece of code works perfectly in a Python interpretor.
There is my code :
import random
for item in range (1,(var.Level +1)):
var.r = random.randint(1,9)
if var.r not in var.x: "<-- Here is my problem. It seams that the test is not done"
var.x.append(str(var.r))
When I print var.x sometimes I have duplicates.
Does someone has an idea about this problem ?
Thanks in advance,
Joan
Comments
Hi Joan,
I can't tell you what is wrong, because you haven't added the part of the code in which you define var.x. The code itself looks alright, I think. An easier way to create a list without duplicates is using `sets`
For example:
var.x = set() for item in range (1,(var.Level +1)): var.x.update(random.randint(1,9)) var.x_list = list(var.x)Hope this helps,
Eduard
Hi eduard,
Thanks for the quick response.
Well, this is because I defined the variable x at the beggining of the experiment. I have an inline script where I define all the variables.
But thanks for the trick, I'll test that and let you know if it is good :)
Joan