Why is the output enclosed in curly braces?

I am making a program where the inputs will be two lists a and b. Both of them should contain integers and can have as many as integers as the user wants. The output will be the integers which have appeared in a but not b.

EXAMPLE
Input
[1,2],[1]
Output
[2]

HERE IS THE CODE

When the input is print(array_diff([1,2],[1])), the output is expected to be [2], but the answer which is coming is {2}. Also when the input is print(array_diff([],[1])), the output is expected to be [], but it is set().

Therefore, I want to know what can I do in order to get the expected output. Every suggestion is appreciated.

THANK YOU

You can use the list() constructor to make a new list of your set just like you used the set() constructor to make sets of your original lists in order to use the .difference() method.
Like this: return list(new_a.difference(new_b)) :wink:
Happy coding!

>>> def array_diff(a, b):
    new_a = set(a)
    new_b = set(b)
    return new_a.difference(new_b)

>>> array_diff([1,2,3],[2,3,4])
{1}
>>> 

Shouldn’t the return value include 4?

>>> def array_diff(a, b):
        new_a = set(a)
        new_b = set(b)
        return new_a.difference(new_b), new_b.difference(new_a)


>>> array_diff([1,2,3],[2,3,4]))
({1}, {4})
>>> 

Good point! I think the return value has to be a list, so perhaps:

def array_diff(a, b):
    new_a = set(a).difference(set(b))
    new_b = set(b).difference(set(a))
    new_a.update(new_b)
    return list(new_a)
print(array_diff([1,2,3],[2,3,4,5]))

Output:
[1, 4, 5]

1 Like