You can try
def uniao(l1,l2):
return list(set(l1+l2))
In this case, you join the two lists, make a set() on them (which will make it have only unique values, but in the {} format) and then move into a list.
You have the unique values of the two lists.
As you can see at this link, set() takes unique values and leaves them sorted (if you have a shuffled list of values).
EDITED:
You can also make a function that can receive a greater variety of lists, passing the function with *args argument and iterating through it as in the example below:
def uniao(*arg):
return list(set([j for i in arg for j in i]))
For example:
l1 = [1,2,3]
l2 = [1,2,5]
l3 = [1,2,3,4]
print(uniao(l1,l2,l3))
>> [1, 2, 3, 4, 5]
So, you can pass the number of lists you want, remembering that this will not work if you pass a number.
In order to use something other than a list, you can also do the code as follows:
def uniao(*arg):
lista = []
for i in arg:
if type(i) == list:
for j in i:
lista.append(j)
else:
lista.append(i)
return list(set(lista))
l1 = [1,2,3]
l2 = [1,2,5]
l3 = [1,2,3,4]
l4 = 6
print(uniao2(l1,l2,l3,l4))
>> [1, 2, 3, 4, 5, 6]
How did you see it going wrong? What test did you do? What was the result obtained and what was expected? In fact, be careful when defining a mutable object as the default value of a parameter, such as the list (read more in When a default argument is evaluated in Python?). In fact, review the indentation of your code in the question; we have no way of knowing if this is wrong in the code or if it was just a formatting error here in the question.
– Woss
It was to appear as a result [1,2,3,5], but it appears [1, 1, 2, 1, 3, 1]
– Gabriel Borges
Well, it depends on the entry lists for that... Try to do the table test of your code.
– Woss