0

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
  • You might want to take a look at this: http://stackoverflow.com/questions/2727650/common-elements-between-two-lists-not-using-sets-in-python – Jayanth Koushik Mar 17 '14 at 08:40
  • 2
    you define `a=[]` and `b=[]` in the start of your function `intersection`. of course the intersection of two empty lists is am empty list. – icedtrees Mar 17 '14 at 08:42
  • why cant you use a set comprehension? dict({i for i in a if i in b}) – sanooj Mar 17 '14 at 09:19

2 Answers2

0

you have for k in range(len(newlist)): where len(newlist) is always 0

you want this

 for i in range(len(a)):
        for j in range(len(b)):
            if a[i] == b[j]:

                already_added = False
                for k in range(len(newlist)):
                    if newlist[k] == a[i]:
                         already_added = True
                         break

                if not already_added:
                    newlist.append(a[i])

It's a rather unpythonic way of doing this though. Why not use a set?

icedtrees right as well.. that's another problem. You can fix icedtrees problem by deleting the two assignments a = [] and b = []. Those lines overwrite the argument values passed to the function and throw away any information you have passed to it.

demented hedgehog
  • 7,007
  • 4
  • 42
  • 49
0

You can also work with sets, that may be more appropriate.

a = list()
b = list()

set_a = set(a)
set_b = set(b)

intersection = set_a.intersection(set_b)   # you have to convert to sets

intersection_list = list(intersection)     # convert back to list if you need it

Note that this doesn't give any guarantees about ordering, and items that come in several copies will be copied only once. Both ordering and multiples are things you will have to consider carefully anyway.

Gijs
  • 10,346
  • 5
  • 27
  • 38