Documentation

Function defined in library random
Side Effects

sample(sequence, k)

samplesequencek

Returns a list with k unique elements sampled from the given sequence.

This is known as 'sampling without replacement'.

sequenceA sequence of elements

kThe number of elements to sample

A list with k random elements from the sequence
Code
sample(['Lugano', 'Zürich', 'Genève'], k=2)
Result
['Lugano', 'Zürich']
Code
sample(['Lugano', 'Zürich', 'Genève'], k=3)
Result
['Zürich', 'Genève', 'Lugano']

See also sample in the official documentation

PyTamaro logo

PyTamaro is a project created by the Lugano Computing Education Research Lab at the Software Institute of USI

Privacy Policy • Platform Version 8e04fca9 (October 23, 2026 at 3:57:58 PM UTC)