So I am trying to write a program that takes two lists as inputs, and then combines common elements into a new list. The problem is my code keeps returning an empty list. Does anyone know what I should write to fix this? Here's my code so far:
def main():
a = list(input("Enter list one: "))
b = list(input("Enter list two: "))
newlist1 = intersection(a,b)
print(newlist1)
def intersection(a,b):
a = []
b = []
newlist = []
for i in range(len(a)):
for j in range(len(b)):
if a[i] == b[j]:
for k in range(len(newlist)):
if newlist[k] != a[i]:
newlist.append(a[i])
return newlist
main()
Edit: Thanks for the comments guys. I have edited out some parts and changed it, still working on it though. I just need to make sure commas are ignored in the list input. Here's what I have so far:
def main():
a = list(input("Enter list one: "))
b = list(input("Enter list two: "))
for i in a:
a[i]=int(a[i])
for i in b:
b[i]=int(b[i])
newlist1 = intersection(a,b)
print(newlist1)
def intersection(a,b):
newlist = [0]
for i in a:
for j in b:
if a[i] == b[j]:
for k in newlist:
if k != a[i]:
newlist.append(a[i])
else:
continue
return newlist