Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Data Science by (18.4k points)

I have 3 lists and want to know how python chooses multiple options from all the lists. 

I tried this code but it gives me only one option in total.

list_1 = [1,3,5]

list_2 = [2,4,6]

list_3 = [10]

random.choice([random.choice(list_1)] + [random.choice(list_2)] + [random.choice(list_3)])

1 Answer

0 votes
by (36.8k points)
edited by

You need to use the random.sample function to get the multiple random values

The syntax is:

 random.sample(list,k) where k represents, number of values to be sampled.

list_1 = [1,3,5]

list_2 = [2,4,6]

list_3 = [10]

random.sample(list_1+list_2+list_3,3)

[edit]

If you want only one from each of the list,

final_list = random.sample(list_1,1)+random.sample(list_2,1)+random.sample(list_3,1)

this can be done using  the random.choice as shown below

final_list =[ random.choice(list_1),random.choice(list_2),random.choice(list_3)]

You can check data science with python course to go through the topic of data science with python.      

Related questions

0 votes
1 answer
0 votes
1 answer
+3 votes
2 answers

Browse Categories

...