Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

In Python, implement a function inBoth () that takes as parameters two lists and

ID: 3791451 • Letter: I

Question

In Python, implement a function inBoth() that takes as parameters two lists and returns True if there is an item that is common to both lists and False otherwise. The list can contain numbers, strings, or a mix of both. The information below shows how you would call the function inBoth() and what it would display for several sample executions:

Hint: Remember to spend time with pen and paper thinking about the problem before diving into the code. (Seeing a pattern here?)

print inBoth (lstA, lstB) False print in Both (lstA, lstc) True print inBoth (lstc, lstB) False print in Both (lstc, lstD) True

Explanation / Answer

def inBoth(list1,list2):
    for i in range(len(list1)):
        if list1[i] in list2:
            return True
    return False


if __name__=="__main__":
    l1=[1,2,3,4]
    l2=[9,34,34,4]
    l3=['python','c','c++']
    l4=['java','c#',100,'python']
    l5=[100,200,300]
    l6=[1,2,3]
    print(inBoth(l1,l2))
    print(inBoth(l3,l4))
    print(inBoth(l5,l6))

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote