0

Why doesn't python allow this syntax:

l1 = range(1,6)
l2 = [0,3]
l1[l2]

I would expect the last line to produce

[1,4]

I understand that I can do this with list comprehensions (as per Getting a sublist of a Python list, with the given indices?), but the syntax I listed above seems natural amd works with np.arrays, and I assume they have a good reason for disallowing it in python; what is it?

Community
  • 1
  • 1
capybaralet
  • 1,757
  • 3
  • 21
  • 31
  • 5
    Ask Guido. I don't think "why hasn't feature X been implemented" is a good question for stackoverflow.. – Blorgbeard Feb 23 '15 at 22:50
  • 1
    Why do you assume that everything you think is a good idea must exist? This feature isn't found in many languages, perhaps the committee doesn't want to add confusion. The difficulty in adding this feature today might be an interesting question though about the Python internals. – Eugene K Feb 23 '15 at 22:51
  • Doesn't strike me as natural, never seen this notation outside of Matlab and Numpy. What are you asking? Whether someone actually considered adding that during Python development? There's no [proposal for extension](https://www.python.org/dev/peps/) about that. – Kos Feb 23 '15 at 22:52
  • 1
    If you use numpy you can have that option availible to you. Numpy allows you to do that for with arrays of indices and arrays of bool, where only True values get returned. Maybe you'll like their arrays better than Python lists? – ljetibo Feb 23 '15 at 22:56
  • The answer to the question is: because no one asked me :D – Antti Haapala -- Слава Україні Feb 23 '15 at 22:57
  • 3
    `import numpy` now @David should be happy – Joran Beasley Feb 23 '15 at 22:57
  • 1
    @Blorgbeard, I was concerned about that, but every other time I've been like: "why doesn't python do this thing???", there has been a good 'technical' answer, but I guess maybe not this time. – capybaralet Feb 23 '15 at 23:09
  • @Kos - (how) can I submit this as a proposal for extension? – capybaralet Feb 23 '15 at 23:09
  • Honestly, the list comprehension is quite compact and readable - plus it can be used for many, many other things. Adding more syntax is not a desirable thing, if there's already an easy/concise way to do something. – robert_x44 Feb 24 '15 at 00:03
  • Why is this syntax necessary? What makes it a good idea? I don't want this cluttering up the language, personally. – fletom Feb 24 '15 at 00:03
  • It isn't necessary, but then hardly any syntax really is. It IS more compact (6 vs. 19 chars!), and, IMO, easier to remember and more intuitive and readable. I've been coding python for 1.5 years and I still forget the syntax for list comprehensions sometimes (because it seems the order could easily be changed, e.g. [x for x in l if cond] vs. [x if cond for x in l]). Actually, accepting both versions there would also be a nice bit of syntactic sugar, IMO (if it wouldn't break anything else). – capybaralet Feb 24 '15 at 20:10
  • @Kos - to answer my own question, see here for how to submit a PEP: https://www.python.org/dev/peps/pep-0001/#submitting-a-pep – capybaralet Feb 24 '15 at 20:17

1 Answers1

1

Python doesn't support that syntax because it doesn't support that syntax. Short of badgering Guido van Rossum, that's the best answer you're going to get.

It's easy to add to a list subclass, though:

class List(list):
    def __getitem__(self, index):
        try:
            return list.__getitem__(self, index)
        except TypeError:
            return List(self[i] for i in index)
    def __setitem__(self, index, value):
        try:
            return list.__setitem__(self, index, value)
        except TypeError:
            value = iter(value)
            for i in index:
                self[i] = next(value)

a = List(range(10))
a.reverse()
print(a[1, 3, 5])    # [8, 6, 4]
a[1, 3, 5] = 1, 2, 3
print(a)             # [9, 1, 7, 2, 5, 3, 3, 2, 1, 0]

The __setitem__ doesn't behave well when you have more values than slots you're filling (it should probably give you an error) but that's tricky to do while also supporting generators, so, meh.

The triviality of implementing it may have bearing on the fact that it's not part of the language; the __getitem__ especially is just a simple list comprehension (or generator expression in this case) which you could just as easily write in your own code wherever you needed to do it.

kindall
  • 178,883
  • 35
  • 278
  • 309