Reputation: 71
I would like to remove one string from a list of strings (some are identical strings). For example in the list [hello, world, world, hello hello], I would like to remove one of the hello's at random. How would I do this?
P.S. In this situation I do not know the position of any of the 'hello' strings in the list
Upvotes: 2
Views: 82
Reputation: 778
original = ['hello', 'world', 'world', 'hello', 'hello']
hello_indices = [i for i, x in enumerate(original) if x == 'hello']
del original[random.choice(hello_indices)]
Upvotes: 5