PEP Idea: Multi-get for lists/tuples and dictionaries (inspired in NumPy)

DL Neil PythonList at DancesWithMice.info
Thu Mar 19 01:22:34 EDT 2020


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' ]

Are you aware, that whilst there is more coverage of "tuple unpacking" 
(at least to my eye), there is also a "list unpacking" feature?

 >>> t = ( 1, 2, 3 )
 >>> a, b, c = t
 >>> print( a, b, c )
1 2 3

 >>> l = [ 1, 2, 3 ]
 >>> a, b, c = l
 >>> print( a, b, c )
1 2 3

and somewhat similarly for dictionaries:

 >>> fname, lname = d[ "first_name" ], d[ "last_name" ]
 >>> fname, lname
('Frances', 'Allen')


That said, I've often wished to be allowed to write:

	d.first_name

for a dict (cf a class/object).

Hmm, I feel a 'utility' coming-on - but first I'll look to see where/how 
such might be used in 'live' code (and be any better than the current 
mechanisms)...

Simple collections are one thing. How would you handle the structure if 
one or more elements contains a second dimension? eg a list within a 
list/a 2D matrix (or if you must, an 'array')?
-- 
Regards =dn


More information about the Python-list mailing list