Last active
August 29, 2015 14:06
-
-
Save pawl/2a4191d1c415a1aee585 to your computer and use it in GitHub Desktop.
Slow Nested For Loop Example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#data | |
old_list = [ | |
{'name': "matthew", 'test_result1': "A", 'test_result2': "A"}, | |
{'name': "steve", 'test_result1': "B", 'test_result2': "D"}, | |
{'name': "dave", 'test_result1': "C", 'test_result2': "F"} | |
] | |
new_list = [ | |
{'name': "steve", 'test_result1': "A", 'test_result2': "F"}, | |
{'name': "matthew", 'test_result1': "B", 'test_result2': "F"}, | |
{'name': "dave", 'test_result1': "C", 'test_result2': "F"} | |
] | |
#this example is slow | |
for old_row in old_list: | |
for new_row in new_list: | |
if (new_row['name'] == old_row['name']): | |
print "found a match" | |
# faster example | |
old_dict = {old_row['name']: old_row for old_row in old_list} | |
del old_list | |
new_dict = {new_row['name']: new_row for new_row in new_list} | |
del new_list | |
for new_key, new_row in new_dict.iteritems(): | |
if old_dict.get(new_key): | |
print "found a match" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment