] returns []

rusi rustompmody at gmail.com
Tue Jan 29 18:32:57 CET 2013

On Jan 29, 6:22 pm, iMath <redstone-c... at 163.com> wrote:
> 在 2013年1月29日星期二UTC+8下午9时21分16秒,iMath写道:
> > why [os.path.join(r'E:\Python', name) for name in []] returns [] ? please explain it in detail !
> >>> [os.path.join(r'E:\Python', name) for name in []]
> []

[Small algebra lesson]
In algebra there is the concept of identity and absorbent.
For example, 1 is the identity for multiply and 0 is the absorbent.
ie for all x: 1 * x = x
and 0 * x = 0
[end algebra lesson]

In the case of lists, [] is an identity for ++ but behaves like an
absorbent for comprehensions.
Modern terminology for 'absorbent' is 'zero-element'. I personally
find the older terminology more useful.

Others have pointed out why operationally [] behaves like an absorbent
in comprehensions.
Ive seen even experienced programmers trip up on this so I believe its
good to know it as an algebraic law in addition to the operational

More information about the Python-list mailing list