Question: (PYTHON) Consider the following function import random # Assumption: listOfList is a list of 0 or more non-empty lists # def randChoices(listOfLists): result = []

(PYTHON) Consider the following function

import random # Assumption: listOfList is a list of 0 or more non-empty lists # def randChoices(listOfLists): result = [] for sublist in listOfLists: chosenItem = random.choice(sublist) result.append(chosenItem) return result 

First, study (and test) randChoices to determine what it does (as part of this, look up what the random module's choice function does). Then re-write randChoices so that it accomplishes the same task with a one-line list comprehension. Thus, the new function will have exactly two lines, the def line and a return line containing a list comprehension expression.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!