0

I've recently took up Python programming and I've written a simple function that takes two lists and returns a new one that does a reunion of the two lists. However when I run the code it returns an empty list. Please help:

x = [1, 2, 3]
y = [4, 5, 6]


def reunion(list_of_numbers1,list_of_numbers2):
    union_list = list()
    for i in range(0,len(list_of_numbers1)):
        if list_of_numbers1[i] in list_of_numbers2 is True:
            union_list.append(i)
            del list_of_numbers1[i]
            del list_of_numbers2[i]
    return union_list
z = reunion(x,y)
print(z)
user3402719
  • 155
  • 1
  • 11
  • Please reproduce your indentation accurately when posting Python code. Badly indented Python code is nonsense. – khelwood Oct 27 '16 at 12:52
  • 1
    This code would obviously return an empty list, as you have no shared elements between the two lists (there's still a few other things wrong with this code as well). – UnholySheep Oct 27 '16 at 12:58
  • What is the expected output of this code? Because you're if statement is not getting processed, as `i` is not in `list_of_numbers2`, therefore nothing will get appended. – RoadRunner Oct 27 '16 at 13:02
  • i will repeatedly take values of elements from the first list and it will be tested if they exist also in the second list – user3402719 Oct 27 '16 at 13:03
  • @user3402719, `i` is the index of each element, not the element. – RoadRunner Oct 27 '16 at 13:05
  • Well... you're not doing that. You are checking if 0, 1, ... are in the second list – kameranis Oct 27 '16 at 13:05

3 Answers3

1

I can see two principal issues with your code:

  1. i iterates over the indices of the first list's elements, so it's those indices, and not the elements themselves, that you're looking up in the second list.

  2. In Python, removing elements of the list you're iterating over is problematic: Python: Removing list element while iterating over list.

Community
  • 1
  • 1
NPE
  • 486,780
  • 108
  • 951
  • 1,012
1

In your example it should return you an empty list, as there are no common numbers.

You want to do:

for number in list_of_numbers1:
    if number in list_of_numbers2:
        union_list.append(number)

This may add duplicates. To avoid those,

for number in list_of_numbers1:
    if number in list_of_numbers2 and number not in union_list:
        union_list.append(number)

However, I would do it like this

return [number for number in list_of_numbers1 if number in list_of_numbers2]
kameranis
  • 284
  • 1
  • 13
1

Something like this will give you a good start:

x = [1, 2, 3, 2]
y = [3, 5, 2, 3]

def reunion(list1, list2):
    result = []
    for number in list1:
        if number in list2 and number not in result:
            result.append(number)
    return result

my_list = reunion(x, y)
print(my_list)

Output:

[2, 3]

Or closer to you're code:

def reunion(list1, list2):
    result = []
    for i in range(0, len(list1)):
        if list1[i] in list2 and list1[i] not in result:
            result.append(list1[i])
    return result
RoadRunner
  • 25,803
  • 6
  • 42
  • 75