Random Should be Part of Iterable

my_list = ['name1', 'name2', ...]
random_name = my_list.random()

Getting Random Elements from a List or an iterable is a basic task and it should be a function of the List/Iterable class.

I havenā€™t gotten random items from a list in my professional career. I have only done that in university settings so it feels too ā€œspecial caseā€ to add it into the interface of all iterables.

1 Like

random.choice(my_list) will do what you want. I donā€™t see any obvious advantage to adding a method that does the same thing and only works on lists.

11 Likes

What Iterable class?

2 Likes

It would be hard to pick one random number generation method to be the one provided.
Its properties would depend on the type of application needed.

Getting an arbitrary item from a sequence is a basic operation, and it is done by indexing, which is special form of subscripting. The random module selection functions use indexing. The fact that an index is considered ā€˜randomā€™ is secondary and irrelevant to the basic function of a sequence.

Python tries to limit builtin classes to truly basic functions. Floats have arithmetic, but exp, sin, etc, etc, etc are left to the math and other modules.

2 Likes