Skip to content

Instantly share code, notes, and snippets.

@svidovich
Last active August 31, 2021 02:03
XOR Two Datasets with Python Sets
import random
import time
def generate_big_list(size) -> list:
big_list = list()
for _ in range(size):
big_list.append(random.randint(1, size * 2))
return big_list
list_size = 1000
big_list_1 = generate_big_list(1000)
big_list_2 = generate_big_list(1000)
xored_list = list()
t0 = time.time()
for item in big_list_1:
if item not in big_list_2:
xored_list.append(item)
for item in big_list_2:
if item not in big_list_1:
xored_list.append(item)
t1 = time.time()
print(f'List xor with {list_size} items by iteration took {t1-t0:.10f}ms')
t0 = time.time()
xored_list = list(set(big_list_1).symmetric_difference(big_list_2))
t1 = time.time()
print(
f'List xor with {list_size} items by set symmetric difference took {t1-t0:.10f}ms'
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment