Knowee
Questions
Features
Study Tools

Write a function mutual_friends(list1, list2) that takes two lists of friends (with the guarantee of there being no repeat names in either list), and returns the number of friends in common based on sets. Your function should behave as follows:>>> mutual_friends(["Bob", "Joe"],["Esmerelda"])0>>> mutual_friends(["Bob", "Joe"],["Bob", "Joe"])2>>> mutual_friends(["Bob", "Joe"],["Bob", "Joe", "Keitha"])2

Question

Write a function mutual_friends(list1, list2) that takes two lists of friends (with the guarantee of there being no repeat names in either list), and returns the number of friends in common based on sets. Your function should behave as follows:>>> mutual_friends(["Bob", "Joe"],["Esmerelda"])0>>> mutual_friends(["Bob", "Joe"],["Bob", "Joe"])2>>> mutual_friends(["Bob", "Joe"],["Bob", "Joe", "Keitha"])2

...expand
🧐 Not the exact question you are looking for?Go ask a question

Solution

Sure, here is a Python function that does what you asked for:

def mutual_friends(list1, list2):
    # Convert both lists to sets
    set1 = set(list1)
    set2 = set(list2)

    # Find the intersection of the two sets
    mutual_friends = set1.intersection(set2)

    # Return the number of mutual friends
    return len(mutual_friends)

This function works by first converting both lists of friends to sets. This is because sets in Python have a built-in method called intersection that returns a new set containing only the elements that are common to both sets. Finally, it returns the number of elements in this new set, which is the number of mutual friends.

This problem has been solved

Similar Questions

Show that in any group of n people there are two who have an identical number of friends within the group. (Friendship is bi-directional – i.e., if A is a friend of B, then B is a friend of A – and nobody is a friend of themselves.) Solve in particular the following two cases individually:Everyone has at least one friend.

Given two lists a, b. Check if two lists have at least one element common in them.

Common elementGiven two lists a, b. Check if two lists have at least one element common in them.

Write a function that returns a set of common elements in two sets.Prototype: def common_elements(set_1, set_2):You are not allowed to import any module

Write a python program to take full name of 2 users separately(name 1 and name 2), and print the number of letters in name 1, then printname 2 number of times equal to the number of letters in name 1.

1/1

Upgrade your grade with Knowee

Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.