Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Supported by

Trouble to create a list without duplicates

edited May 2021 in OpenSesame

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

  • edited May 2021

    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

    Buy Me A Coffee

  • 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

Sign In or Register to comment.