Search code examples
pythondictionaryrandom

Python Random.Choice EXCEPT One Option


I am trying to use random.choice() to select an item from a dictionary, however, I would like one of the items to be ignored entirely. For example:

mutationMarkers = {0: "original", 1: "point_mutation", 2: "frameshift_insertion", 
                   3: "frameshift_deletion", 4: "copy_number_addition", 
                   5: "copy_number_subtraction"}

mutator = choice(list(markers)) # output is 0, 1, 2, 3, 4, 5

Is it possible to use random.choice and ignore {0: "original"}?


Solution

  • You can use a list comprehension:

    mutator = choice([x for x in mutationMarkers if x != 0])