-2

Hey guys I dont want the console to print duplicated letters can anyone solve this?

import random

random_list = ["a","b","c","d","e","f"]

for i in range(5):
    test = random.choices(random_list)
    print(test)
homie
  • 1
  • 1
    Use `random.sample` (sample without replacement) with `k=5` and then iterate over the resultant choice set (or just print the entire thing, whatever). – wkl Aug 22 '22 at 00:54
  • Welcome to Stack Overflow. Please read the [documentation](https://docs.python.org/3/library/random.html) for the `random` standard library module. In particular, notice that it tells you that `random.choices` will give results "with replacement", meaning that it is allowed to repeat the values. (Of course, this code asks it for *one* value at a time, in a loop, which defeats the purpose anyway.) To get the values without replacement, you want `random.sample` instead. – Karl Knechtel Aug 22 '22 at 07:10
  • In the future, [please try](https://meta.stackoverflow.com/questions/261592/) to [look up answers yourself](https://duckduckgo.com/?q=site%3Astackoverflow.com+python+choose+without+duplicates) before posting. – Karl Knechtel Aug 22 '22 at 07:11

2 Answers2

-1

remove the item after you print it with .pop()

import random

random_list = ["a","b","c","d","e","f"]

for i in range(5):
    test = random.choices(random_list)[0]
    random_list.pop(random_list.index(test))
    print(test)
Chris B
  • 24
  • 3
-1

You have different options.

  1. simply remove the item from the existing list.

  2. create a used list and check that items are not in that list.

here is an example of method 2:

import random

random_list = ["a","b","c","d","e","f"]
used_list = []

for i in range(5):
    test = random.choices(random_list)
    if test not in used_list:
        print(test)
    used_list.append(test)

D.L
  • 4,339
  • 5
  • 22
  • 45