On 1/3/21 12:38 PM, Mats Wichmann wrote: > On 1/3/21 5:30 AM, Bischoop wrote: >> On 2021-01-02, Stefan Ram <r...@zedat.fu-berlin.de> wrote: >>> Bischoop <bisch...@vimart.net> writes: >>>> On 2021-01-02, Stefan Ram <r...@zedat.fu-berlin.de> wrote: >>>>> Otherweise, I'd go this way without a dictionary. >>>>> import random >>>>> animal = ['koala', 'kangaroo'] >>>>> fruit = ['banana', 'apple'] >>>>> kinds = [animal,fruit] >>>>> kind = random.choice( kinds ) >>>>> result = random.choice( kind ) >>>>> print( result ) >>>> I had that solution in mind but I thought that one is not good >>>> programming style or not Pythonin :-) >>> >>> I do not see any stylistic problem when you use this approach >>> with "nested lists". List indexing by a number should even be >>> faster than indexing a dictionary. >>> >>> >> Now I know that's ok, seems I was ovethingking while solution was so >> simply. >> >> -- >> Thanks >> > > You don't really need to do this as a two-level selection: > > import random > animal = ['koala', 'kangaroo'] > fruit = ['banana', 'apple'] > result = random.choice(animal + fruit) > print(result)
It depends on what distribution of results you want. Since the example had two equal length list is doesn't matter, but if, say, there were many more animals then fruit, your method would produce an animal more often than a fruit, but the two level method will make the distribution 50% fruits, 50% animals independent on the number in each category. Which is the right answer is problem specific. -- Richard Damon -- https://mail.python.org/mailman/listinfo/python-list