How does python optimize conditional list comprehensions

前端 未结 1 1540
一整个雨季
一整个雨季 2021-02-05 19:23

I read about List comprehension without [ ] in Python so now I know that

\'\'.join([str(x) for x in mylist])

is faster than

\'\         


        
相关标签:
1条回答
  • 2021-02-05 19:47

    List comprehensions don't pre-size the list, even when they totally could. You're assuming the presence of an optimization that isn't actually done.

    The list comprehension is faster because all the iterator machinery and the work of entering and exiting the genexp stack frame has a cost. The list comprehension doesn't need to pay that cost.

    0 讨论(0)
提交回复
热议问题