samedi 10 décembre 2016

How to randomly select a tuple from a list?

I'm randomly creating a list of tuples from two tuples, like so:

tuple1 = ('green','yellow','blue','orange','indigo','violet')
tuple2 = ('tree',2,'horse',4,6,1,'banana')

mylist = [(t1,t2) for item1 in tuple1 for item2 in tuple2]

which of course gives me something like:

[('green','tree'),('yellow', 2)] and so on.

But then, I want to randomly select one two-item tuple from the generated mylist. In other words, return something like ('green',2).

How do I randomly select one two-item tuple from a list of them? I tried the following, but it isn't working:

my_single_tuple = random.choice(mylist.pop())

I'd be grateful for any clues or suggestions.




Aucun commentaire:

Enregistrer un commentaire