8
I have two lists whose contents refer to a arquivo1.txt and the other one arquivo2.txt, there are differences in the contents of the files and wish to search only the data that is in file2.txt and is not in file1.txt, I don’t care the other way around.
It is possible to solve this with a loop which compares if the element 0 belonging to the arquivo2.txt is present in some position of the archive1.txt list, the problem is that you would have to go through that whole list.
There’s a way easier and simplified to make this match of lists in Python?
Note only that duplicates will be removed, for example if you have two elements
"hi"
inlista_arquivo_2
(and there is a"hi"
inlista_arquivo_1
) the final result will contain only one"hi"
instead of two and the order will also not be preserved.– BrunoRB
Note that the situation that "keeps the elements repeated" implicitly includes the loop that traverses the entire list_arquivo_1 for each item in list_arquivo_2 - the algorithm is O(N²). The great advantage of the example with sets is that it searches in sets (
set
) in Python are made in constant time.– jsbueno