Ask coding questions

← Back to all posts
Sets problems
azza1551

I wanna help with writing a function that takes two lists and outputs a list that contains all common elements in each list. use the & operator (or
the setA = setB.intersection(setC) notation)

define the function problemTwo(listA, listB)
This should output: [3, 4]

list1 = [1, 2, 3, 4]
list2 = [3, 4, 5, 6]
result2 = problemTwo(list1, list2)
print(result2)

Voters
azza1551
Comments
hotnewtop
ZetaCommand

Try something like:

list1=[1,2,1,4]
list2=[2,2,1,5]
def findCommon(l1,l2):
l1String=""
l2String=""
newString=[]
for i in range(len(l1)):
l1String+=str(l1[i])
for i in range(len(l2)):
l2String+=str(l2[i])
for i in l1String:
if i in l2String:
newString.append(i)

return(newString)

print(findCommon(list1,list2))

Highwayman

That sounds like a homework problem….