Santiago Basulto <santiago.basu...@gmail.com> writes: > Hello community. I have an idea to share with the list to see what you all > think about it. > > I happen to use both Python for Data Science (with our regular friends > NumPy and Pandas) as well as for scripting and backend development. Every > time I'm working in server-side Python (not the PyData stack), I find > 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': 'fal...@ibm.com' > } > fname, lname = d[['first_name', 'last_name']] > > I really like the syntax of boolean arrays too, but considering we have > list comprehensions, seems a little more difficult to sell. > > I'd love to see if this is something people would support, and see if > there's room to submit a PEP.
How about implementing it yourself: In [35]: class MultiDict(dict): ... def __getitem__(self, idx): ... if isinstance(idx, list): ... return [self[i] for i in idx] ... return super().__getitem__(idx) In [36]: d = MultiDict({ ... 'first_name': 'Frances', ... 'last_name': 'Allen', ... 'email': 'fal...@ibm.com' ... }) In [37]: d Out[37]: {'first_name': 'Frances', 'last_name': 'Allen', 'email': 'fal...@ibm.com'} In [38]: d['email'] Out[38]: 'fal...@ibm.com' In [39]: d[['first_name', 'last_name']] Out[39]: ['Frances', 'Allen'] The implementation of other methods, like __setitem__ and __init__, and maybe some more subtle details like exceptions, is left as an exercise for the reader. -- Pieter van Oostrum www: http://pieter.vanoostrum.org/ PGP key: [8DAE142BE17999C4] -- https://mail.python.org/mailman/listinfo/python-list