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
th
Python
ZetaCommandTry 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))2 years ago
HighwaymanThat sounds like a homework problem….2 years ago
1 - How to write a function that takes a dictionary and a key and determines if that key is in the dictionary (i.e. the function outputs True if the k
Python
Coder100to check if a key is in a dictionary, use the in keyword
def keyisin(d, k):
return k in d
to join 2 dictionaries together, use a for loop!
def join_dicts(a, b):
out = {}
for keya in a:
out[keya] = a[keya]
for keyb in b:
out[keyb] = b[keyb]
return out
`2 years ago
CosmicBeardef function1(dict,key):
if key in dict:
return True
return False
def function2(dict1,dict2):
for key in dict2:
dict1[key] = dict2[key]
return dict12 years ago