PEP Idea: Multi-get for lists/tuples and dictionaries (inspired in NumPy)
Rhodri James
rhodri at kynesim.co.uk
Thu Mar 19 10:24:35 EDT 2020
On 19/03/2020 13:00, Peter J. Holzer wrote:
> 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:
[snip]>> 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']
I have to say I don't think that's more compact at all. It's too wide
to be compact. I think the second version is more readable (and the
third version, where you factored out the common lookup is better still).
--
Rhodri James *-* Kynesim Ltd
More information about the Python-list
mailing list