Are Python3.5 tuple comprehension really this limited?

后端 未结 2 649
滥情空心
滥情空心 2021-01-14 07:51

I\'ve been loving the tuple comprehensions added to Python3.5:

In [128]: *(x for x in range(5)),
Out[128]: (0, 1, 2, 3, 4)

However, when I

2条回答
  •  醉话见心
    2021-01-14 08:45

    Pass a generator expression into the tuple() constructor, since there are no tuple-comprehensions:

    {idx: tuple(x for x in range(5)) for idx in range(5)}
    

    Tuple-comprehensions don't exist, but even though list-comprehensions do ([... for ... in ...]) they are similar to*: list(... for ... in ...).


    *list comprehensions are actually faster than a generator expression passed into a constructor function as executing functions is expensive in Python

提交回复
热议问题