Selecting elements from a list

Martin Christensen knightsofspamalot-factotum at gvdnet.dk
Thu Sep 4 17:59:29 EDT 2003


-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Howdy!

Suppose I have a list A containing elements that I want to put into
list B if they satisfy some property. The obvious way of doing it
would be,

B = []
for i in A:
    if A.property():
       B.append(i)

Now, list comprehensions, map() etc. can make a lot of list operations
easier, but I haven't found a shorter, more elegant way of doing this.
Have I missed something, or will I have to stick to my explicit loops?

Martin

- -- 
Homepage:       http://www.cs.auc.dk/~factotum/
GPG public key: http://www.cs.auc.dk/~factotum/gpgkey.txt
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.2 (GNU/Linux)
Comment: Using Mailcrypt+GnuPG <http://www.gnupg.org>

iEYEARECAAYFAj9XtcEACgkQYu1fMmOQldVX0wCeJ7gxV82QVCqRZ3r44vbkFquf
M3QAnRGXnV3l/KslD7LNxT9roVQhGgJM
=aVpW
-----END PGP SIGNATURE-----




More information about the Python-list mailing list