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.

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.

10 Likes

What Iterable class?

1 Like

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.

1 Like