Generator Expressions vs. List Comprehension

前端 未结 9 1820
梦如初夏
梦如初夏 2020-11-21 06:56

When should you use generator expressions and when should you use list comprehensions in Python?

# Generator expression
(x*2 for x in range(256))

# List com         


        
9条回答
  •  时光取名叫无心
    2020-11-21 07:10

    Sometimes you can get away with the tee function from itertools, it returns multiple iterators for the same generator that can be used independently.

提交回复
热议问题