PEP Idea: Multi-get for lists/tuples and dictionaries (inspired in NumPy)
Peter J. Holzer
hjp-python at hjp.at
Thu Mar 19 09:00:10 EDT 2020
On 2020-03-19 18:22:34 +1300, DL Neil via Python-list wrote:
> On 19/03/20 3:28 PM, Santiago Basulto wrote:
> > myself missing A LOT features from NumPy, like fancy indexing or
> > boolean arrays.
> > So, has it ever been considered to bake into Python's builtin list and
> > dictionary types functionality inspired by NumPy? I think multi indexing
> > alone would be huge addition. A few examples:
> > For lists and tuples:
> > >>> l = ['a', 'b', 'c']
> > >>> l[[0, -1]]
> > ['a', 'c']
> > For dictionaries it'd even be more useful:
> > d = {
> > 'first_name': 'Frances',
> > 'last_name': 'Allen',
> > 'email': 'fallen at ibm.com'
> > }
> > fname, lname = d[['first_name', 'last_name']]
>
>
> I fear that I'm missing your point.
>
> How is
> l[[0, -1]] or fname, lname = d[['first_name', 'last_name']]
> any better than
> l[ 0 ], l[ -1 ] or
> fname = d[ 'first_name' ]
> lname = d[ 'last_name' ]
It's more compact, especially, if "d" isn't a one-character variable,
but an expression:
fname, lname = db[people].employee.object.get(pk=1234)[['first_name', 'last_name']]
vs.
fname = db[people].employee.object.get(pk=1234)['first_name']
lname = db[people].employee.object.get(pk=1234)['last_name']
Plus the latter probably performs two database lookups, so you would
want to write:
person = db[people].employee.object.get(pk=1234)
fname = person['first_name']
lname = person['last_name']
(This is one of the Perl features I missed when I started using Python)
hp
--
_ | Peter J. Holzer | Story must make more sense than reality.
|_|_) | |
| | | hjp at hjp.at | -- Charles Stross, "Creative writing
__/ | http://www.hjp.at/ | challenge!"
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 833 bytes
Desc: not available
URL: <http://mail.python.org/pipermail/python-list/attachments/20200319/6fbff685/attachment.sig>
More information about the Python-list
mailing list