'how to choose a random 5 strings out of 10

I have one list consisting of 10 questions. How do I choose 5 (max) at random and there can be no doubles - in python.

I have tried using:

from random import choice

strings = ['aa','bb','cc','dd','ee']
print(choice(strings))

But have no Idea how to choose 5 and have no doubles.



Solution 1:[1]

The method random.choice() draws samples with replacement. What you want is drawing samples without replacment. This is what random.sample() does.

Example with 5 samples drawn:

print(random.sample(strings, 5))

See the documentation.

Solution 2:[2]

This should work:

import random
random.sample(strings, 5)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 ypnos
Solution 2 D.Manasreh