List comprehension without [ ] in Python

后端 未结 7 2257
悲&欢浪女
悲&欢浪女 2020-11-21 05:28

Joining a list:

>>> \'\'.join([ str(_) for _ in xrange(10) ])
\'0123456789\'

join must take an iterable.

Appa

7条回答
  •  野趣味
    野趣味 (楼主)
    2020-11-21 05:48

    Your second example uses a generator expression rather than a list comprehension. The difference is that with the list comprehension, a list is completely built and passed to .join(). With the generator expression, items are generated one by one and consumed by .join(). The latter uses less memory and is generally faster.

    As it happens, the list constructor will happily consume any iterable, including a generator expression. So:

    [str(n) for n in xrange(10)]
    

    is just "syntactic sugar" for:

    list(str(n) for n in xrange(10))
    

    In other words, a list comprehension is just a generator expression that is turned into a list.

提交回复
热议问题