Dhar_
Dhar_

Reputation: 71

How do I remove one string from a list

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

Answers (1)

Alex Fish
Alex Fish

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

Related Questions