Skip to content

Instantly share code, notes, and snippets.

@jimbrayrcp
Created April 21, 2021 21:01
Show Gist options
  • Save jimbrayrcp/90fb155536c08e2517155a7bce2df636 to your computer and use it in GitHub Desktop.
Save jimbrayrcp/90fb155536c08e2517155a7bce2df636 to your computer and use it in GitHub Desktop.
Python List Compensation to compare single list of numbers in two text files extracting the matching numbers into a single list
# # PYTHON LIST COMPENSATION
# compare single list of numbers
# in two text files extracting the matching numbers into a single list
# NOTE: in file1 & file2 the numbers are a single column in each list.
with open('file1.txt') as f:
file1 = [int(n) for n in f]
with open("file2.txt") as f:
file2 = [int(n) for n in f]
result = [element for element in file1 if element in file2]
print(result)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment