Skip to content

Instantly share code, notes, and snippets.

@pawl
Last active August 29, 2015 14:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pawl/2a4191d1c415a1aee585 to your computer and use it in GitHub Desktop.
Save pawl/2a4191d1c415a1aee585 to your computer and use it in GitHub Desktop.
Slow Nested For Loop Example
#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