1
$ lst = ['a', 'b', 'c', 'd', 'e']
$ lst[[0,3]]
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: list indices must be integers or slices, not list

Python list does not support indexing with integer list. R supports it. I am from R. So I thought, why not make it possible?

So here's my code.

class list2(list):
    def __getitem__(self, x):
        if isinstance(x, __builtins__.list):
            #print(x)
            return [__builtins__.list.__getitem__(self, y) for y in x]
        else:
            return __builtins__.list.__getitem__(self,x)
            
l = list2(['a', 'b', 'c', 'd', 'e'])
l[[2,3]]

It works as I expected.

My question is "can any side effect occur so that I need to take caution in using my list2()?"

I don't think there is since python list does not allow list in lst[] or .__getitem__() in the first place but who knows?

UPDATES

I found one case that does not extend smoothly.

del lst[1] or del lst[::2] works fine but del lst[[1,3]] does not work.

But I don't think this is a serious problem because it does not break compatibility of existing source.

KH Kim
  • 1,155
  • 1
  • 7
  • 14
  • 4
    np.arrays pretty much do this already... – Julien Aug 19 '21 at 05:09
  • 2
    The side effect for any other pythonista is: "HUH?" Whats that? You loose (or would have to recreate) the "normal" ways to generate list like: `c = [a*2 for a in range(20)]` - I do not see any benefit - it is also 1 letter longer then normal slicing – Patrick Artner Aug 19 '21 at 05:09
  • 1
    Why are you using `__builtins__`??? As stated already, if you want R-like data structures, use `numpy` and `pandas`, don't try to shoehorn the Python built-ins to something like R. – juanpa.arrivillaga Aug 19 '21 at 05:27
  • No, you cannot change how Python source code literals work, not without changing the Python runtime/ compiler itself. – juanpa.arrivillaga Aug 19 '21 at 05:28
  • @Artner I don't get it. Why do I have to **recreate** anything similar to `c=[a*2 for a in range(20)]`? I just proposed replacing `.__getitem()__` method. Everything else is intact. – KH Kim Aug 19 '21 at 06:00
  • @juanpa.arrivillaga I thought so but I had a hope that someone might come up with a brilliant idea that no body ever thought of like other questions I had seen, in which someone confidently declares that it was impossible but someone else came later with a brilliant answer. ex) https://stackoverflow.com/questions/406230/regular-expression-to-match-a-line-that-doesnt-contain-a-word – KH Kim Aug 19 '21 at 06:07

1 Answers1

-1

If your intended output is 'c' and 'd', try this:

import pandas as pd
lst = pd.Series( ['a', 'b', 'c', 'd', 'e'])
lst.loc[[2,3]]

Pandas is sort of the "make Python work like R" library.

Acccumulation
  • 3,491
  • 1
  • 8
  • 12
  • Of course I know pandas or numpy. Anyway if you start from a list, you need to do something like `np.array(lst)[[2,3]]` or `pd.Series(lst).loc[[2,3]]` which is quite cumbersome. And `list.__getitem__()` does nothing with list argument so it could be an extension... maybe I better propose python development team... – KH Kim Aug 19 '21 at 05:57