dimanche 13 septembre 2020

Get a random value from a list and its position at the same time in Python

I was wondering if there's a way in Python to get from a list, a random value of it and its position at the same time.

I know I can do this in two steps:

list_index = random.randrange(len(my_list))
list_value = my_list[index]

But with this implementation if, for example, there're two (or more) indentical values, Python outputs me the 1st position:

Example:

    [3 5 6 8 5 7]
    Random's output value = 5
    Position = 1

    But 5 value is also in position 4

How can I implement this? Is it feasible? I've been thinking and searching on the web so much, but couldn't find anything.

Really thank you in advance.

Alex




Aucun commentaire:

Enregistrer un commentaire