Are list comprehensions syntactic sugar for `list(generator expression)` in Python 3?

前端 未结 4 476
有刺的猬
有刺的猬 2020-12-06 03:54

In Python 3, is a list comprehension simply syntactic sugar for a generator expression fed into the list function?

e.g. is the following code:



        
4条回答
  •  鱼传尺愫
    2020-12-06 04:39

    They aren't the same, list() will evaluate what ever is given to it after what is in the parentheses has finished executing, not before.

    The [] in python is a bit magical, it tells python to wrap what ever is inside it as a list, more like a type hint for the language.

提交回复
热议问题