Mar-28-2020, 05:57 AM
This is one way to do it
And this is working with lists - order and repeating elements are preserved
# convert to sets result = set(dia).union(set(mots)) print(result) print(list(result)) result2 = set(dia).intersection(set(mots)) print(result2) print(list(result2))However, this will not preserve order and set have unique elements, so if there are duplicates in the list (same element more than one time in list) they will be lost
And this is working with lists - order and repeating elements are preserved
# if order is imporatnt result = [element for element in dia if element in set(mots)] # this is list comprehension, but it can be replaced by regular loop print(result) dia.extend(mots) print(dia)
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs