generator-expression

List comprehension vs generator expression's weird timeit results?

不羁的心 提交于 2019-11-26 01:07:54
问题 I was answering this question, I preferred generator expression here and used this, which I thought would be faster as generator doesn\'t need to create the whole list first: >>> lis=[[\'a\',\'b\',\'c\'],[\'d\',\'e\',\'f\']] >>> \'d\' in (y for x in lis for y in x) True And Levon used list comprehension in his solution, >>> lis = [[\'a\',\'b\',\'c\'],[\'d\',\'e\',\'f\']] >>> \'d\' in [j for i in mylist for j in i] True But when I did the timeit results for these LC was faster than generator: