Member-only story
Interview question: Randomise the items in a list in place in Python
If you are going to attend an interview as a Python developer or data scientist, you will be asked questions to determine how well you know your subject.
One question that you may be asked is to randomise the items in a list in place.
There is a very simple way that you can accomplish this task with the random library’s shuffle function. The shuffle reorganises the order of items in a list.
Below is a screenshot of how to use random’s shuffle function to randomise a list:-
A second way to shuffle a list in place in Python is to use random library’s sample function. Sample returns a list with the number of requested elements in the list.
Below is a screenshot of how to use random’s sample function to randomise a list:-
A third way to use random’s choice function. Choice returns one random element from a list.