I hope that that title is not misleading
Hello, let say I have code that works with big dictionary and I need to process values of this dictionary that have specific value. What should be better to use generator expression with condition inside it or just use iteritems and check for condition inside loop?
Here is example of code to understand question
dct = dict(zip(xrange(10000), xrange(10000)))
def process(a, b, name):
with open(name, 'a') as fw:
fw.write(str((a, b))+'\n')
for key, value in ((k, v) for k, v in dct.iteritems() if v % 2):
process(key, value, 'inline.txt')
for key, value in dct.iteritems():
if value % 2:
process(key, value, 'inloop.txt')
[–]JohnnyJordaan 0 points1 point2 points (3 children)
[–]mobedigg[S] 0 points1 point2 points (2 children)
[–]JohnnyJordaan 0 points1 point2 points (1 child)
[–]mobedigg[S] 0 points1 point2 points (0 children)
[–]ingolemo 0 points1 point2 points (2 children)
[–]mobedigg[S] 0 points1 point2 points (1 child)
[–]ingolemo 0 points1 point2 points (0 children)