python tip

Get Sample from List

This is another awesome tip code that will help you get sample data from a list with any quantity. Check out the below example code.

# Get Sampleimport random#example 1mylist = [100, 200, 300, 400, 500, 600]print(random.sample(mylist, 2)) # [100, 400]#example 2mylist = ["a", "b", "c", "d", "e", "f"]print(random.sample(mylist, 3)) # ['d', 'c', 'b']