0

I'm trying to randomized some words in Python, but I get lines with repeated terms. How can I avoid it?

My code:


words = ['blue,', 'orange,', 'yellow,', 'white,','pink,']

list=[]
for i in range(90):
    r = ''.join(random.choices(words, k=random.randint(1, 5)))
    if r not in list: list.append(r) 

What I get:


orange,white,orange,white,blue,  <-- Here Orange & white are twice

Thanks!

Fantailed
  • 98
  • 5
Mind3943
  • 1
  • 2
  • 1
    `random.randint()` samples without replacement. Here's an example of how to get random numbers without replacement. https://stackoverflow.com/questions/8505651/non-repetitive-random-number-in-numpy – Ari Anisfeld Aug 18 '20 at 23:30
  • Thanks @AriAnisfeld – Mind3943 Aug 18 '20 at 23:36

0 Answers0