3

I know "how" to use the : operator to extract data from a list in python.

Eg:

l = [1,2,3]
print(l[0:1])
print(l[-1:])

Yields:

[1]
[3]

But, how does python literally interpret the :?

Why can't I set a variable equal to : and then call it?

Eg:

x = :
print(l[x])

Would yield:

[1, 2, 3]

Hoping someone who can go deeper on how python works can offer some insight.

Thanks!

Konrad Rudolph
  • 530,221
  • 131
  • 937
  • 1,214
jason m
  • 6,519
  • 20
  • 69
  • 122
  • 8
    "Why can't I set a variable equal to `:` and then call it?" - Same reason you can't do `x = [; print(lx0])`. You can't assign syntax to a variable. – user2357112 Dec 16 '20 at 20:33
  • 1
    ...can't assign syntax to a regular string-type variable (and expect it to be interpreted without some kind of explicit `eval` equivalent) in almost **any** language at all -- and the languages in which you can are completely unsuited to any kind of code that process untrusted data. This isn't strictly a Python thing -- I doubt you could name me a language where it _isn't_ true (I can think of one, but it's pretty severely niche). – Charles Duffy Dec 16 '20 at 20:34
  • 1
    Does this answer your question? [Understanding slice notation](https://stackoverflow.com/questions/509211/understanding-slice-notation) – brianrice Dec 16 '20 at 20:35
  • @user2357112supportsMonica - I think it would be more accurate to say that the compiler doesn't support it at the moment. But why shouldn't `:` be converted to `slice(0, None)` just like what happens in `x[:]`? It may be that there is something unresolvable in the syntax, it would be pretty cool to do it. – tdelaney Dec 16 '20 at 20:44
  • @tdelaney: That's ambiguous with other uses of `:`, like dictionaries and annotations. Before the PEG parser, I think the biggest blocker would have been disambiguating the `:` in something like `for x in 1, 2, 3:`. Now that the PEG parser is in, you could probably handle it similarly to how Python disambiguates the many uses of the comma, but it wouldn't be worth it at all. Building slice objects outside of a slicing expression is rare and already handled by `slice`. – user2357112 Dec 16 '20 at 21:00
  • Either way, you definitely wouldn't be able to do something like `x = :; print(l[1x])`. – user2357112 Dec 16 '20 at 21:02
  • @user2357112supportsMonica - I was digging into this a bit more. From the [Slicings](https://docs.python.org/3/reference/expressions.html#slicings) doc, _anything that looks like an expression list also looks like a slice list_ and there is a hack to prioritize slice within a subcription. That wouldn't work generally. Where this goes with the PEG parser... that's above my pay grade. – tdelaney Dec 16 '20 at 21:04
  • @tdelaney: The weird thing about that doc is, there is no prioritization. All subscriptions are parsed using the general slicing interpretation, because it's exactly equivalent to the expression-list interpretation when there are no slices. There's no separate grammar rule or handling for an expression list between the indexing brackets. – user2357112 Dec 16 '20 at 21:11
  • `python` (similar to other languages) is a program which reads your code and it tries to recognize elements in code (it is called `parser`) and it has defined rules which decide what is correct and what is wrong - and one of this rule decides that `x = :` is not correct code. BTW: using modules like [PLY](https://www.dabeaz.com/ply/) or [SLY](https://sly.readthedocs.io/en/latest/) you can create own language with own rules and you can have rules which allows to do `x = :` – furas Dec 17 '20 at 02:08

1 Answers1

4

: is shorthand for the slice class. You can assign instances of that class to variables for what you're trying to:

>>> l = [1, 2, 3]
>>> l[1:3]
[2, 3]
>>> l[slice(1, 3)]
[2, 3]
>>> x = slice(0, 2)
>>> l[x]
[1, 2]
kevingessner
  • 18,559
  • 5
  • 43
  • 63