Skip to content
Advertisement

Shuffling a list of objects

How do I shuffle a list of objects? I tried random.shuffle:

JavaScript

But it outputs:

JavaScript

Advertisement

Answer

random.shuffle should work. Here’s an example, where the objects are lists:

JavaScript

Note that shuffle works in place, and returns None.

More generally in Python, mutable objects can be passed into functions, and when a function mutates those objects, the standard is to return None (rather than, say, the mutated object).

User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement