22.0k views
1 vote
The learning center teaches 2 classes. Each class has its list of student names.

Some students are taking one class, some are taking both the classes. Write a function (getResult) with the list parameters (classOne and classTwo). The function will return the the list of students who are taking both classes. (PYTHON)

1 Answer

1 vote

Sure, here's an example Python function that takes two lists of student names as arguments and returns a list of students who are taking both classes:

def getResult(classOne, classTwo):

# use set intersection to find students who are in both classes

studentsTakingBoth = list(set(classOne) & set(classTwo))

return studentsTakingBoth

Here's an example usage of the function:

classOne = ['Alice', 'Bob', 'Charlie', 'David']

classTwo = ['Bob', 'David', 'Eve', 'Frank']

studentsTakingBoth = getResult(classOne, classTwo)

print(studentsTakingBoth) # should print ['Bob', 'David']

In this example, the getResult function takes two lists classOne and classTwo as arguments, and finds the intersection of the two sets of students using the set intersection operator &. The resulting list is then returned. Note that we first convert the set intersection result into a list before returning it, since the set data type does not preserve the order of elements.

User Edwardth
by
8.5k points