# Question about range operator (":")

**URL:** <https://discourse.julialang.org/t/question-about-range-operator/111247>\
**Category:** General Usage\
**Tags:** question\
**Created:** [March 6, 2024, 3:57pm UTC](https://discourse.julialang.org/t/question-about-range-operator/111247 "2024-03-06T15:57:49Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![opera\_malenky](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/opera_malenky/32/8213_2.png) [@opera\_malenky](https://discourse.julialang.org/u/opera_malenky)\
**Post date:** [March 6, 2024, 3:57pm UTC](https://discourse.julialang.org/t/question-about-range-operator/111247/1 "2024-03-06T15:57:49Z")

</div>

I was testing some code and noticed something that seemed odd to me when trying to use the range operator (“:”).

```julia
julia> 1:5
1:5

```

but

```julia
julia> 5:1
5:4

```

I don’t see anything in the help for `:` that suggests different behavior if `a<b` or `a>b` for `a:b`, although in the help for `range` I do see this statement: `If length is not specified and stop - start is not an integer multiple of step, a range that ends before stop will be produced.`

So I guess that the issue is that a range that goes from a larger number to a smaller is giving a negative number, and is not considered an integer multiple of step? Is this expected behavior? I’d sort of expect to be able to create a range from a larger number to a smaller.

---

<div class="post-metadata">

**Author:** ![mkitti](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mkitti/32/12459_2.png) [@mkitti](https://discourse.julialang.org/u/mkitti)\
**Post date:** [March 6, 2024, 4:06pm UTC](https://discourse.julialang.org/t/question-about-range-operator/111247/2 "2024-03-06T16:06:45Z")

</div>

Note the part about `step` defaulting to `1`.

```julia
help?> :
search: : :: ?: >: <:

  (:)(start, [step], stop)

  Range operator. a:b constructs a range from a to b with a step
  size equal to 1, which produces:

    • a UnitRange when a and b are integers, or

    • a StepRange when a and b are characters, or

    • a StepRangeLen when a and/or b are floating-point.

  a:s:b is similar but uses a step size of s (a StepRange or
  StepRangeLen). See also range for more control.

  The operator : is also used in indexing to select whole
  dimensions, e.g. in A[:, 1].

  : is also used to quote code, e.g. :(x + y) isa Expr and :x isa
  Symbol. Since :2 isa Int, it does not create a range in indexing:
  v[:2] == v[2] != v[begin:2].

```

This is the expected and documented behavior. If you want to go backwards, you need a step of `-1`.

```julia-repl
julia> r = 5:1
5:4

julia> step(r)
1

julia> typeof(r)
UnitRange{Int64}

julia> r = 5:-1:1
5:-1:1

julia> step(r)
-1

julia> typeof(r)
StepRange{Int64, Int64}

julia> collect(r)
5-element Vector{Int64}:
 5
 4
 3
 2
 1

```

---

<div class="post-metadata">

**Author:** ![opera\_malenky](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/opera_malenky/32/8213_2.png) [@opera\_malenky](https://discourse.julialang.org/u/opera_malenky)\
**Post date:** [March 6, 2024, 4:12pm UTC](https://discourse.julialang.org/t/question-about-range-operator/111247/3 "2024-03-06T16:12:08Z")

</div>

Ah, I see. So `5:-1:1` works just fine. I guess in my head, I only ever think of step sizes as magnitudes, not directions. Guess I’m still used to how R does it.

Anyway, thanks for the help!

---

<div class="post-metadata">

**Author:** ![rocco\_sprmnt21](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rocco_sprmnt21/32/20127_2.png) [@rocco\_sprmnt21](https://discourse.julialang.org/u/rocco_sprmnt21)\
**Post date:** [March 6, 2024, 5:00pm UTC](https://discourse.julialang.org/t/question-about-range-operator/111247/4 "2024-03-06T17:00:56Z")

</div>

[here](https://stackoverflow.com/questions/42965709/how-does-julia-interpret-101) some more thoughts on the topic

[here the code](https://github.com/JuliaLang/julia/blob/cc917b56874414da41de01ea3d363ffe980f7cee/base/range.jl#L149)

```julia
  ifelse(stop >= start, stop, convert(T,start-oneunit(stop-start)))

```

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [March 6, 2024, 5:27pm UTC](https://discourse.julialang.org/t/question-about-range-operator/111247/5 "2024-03-06T17:27:49Z")

</div>

See also related discussions:

- ['Negative' UnitRange in Julia?](https://discourse.julialang.org/t/negative-unitrange-in-julia/72864)
- [The colon punctuation: Why doesn't 5:1 return [5, 4, 3, 2, 1]?](https://discourse.julialang.org/t/the-colon-punctuation-why-doesnt-5-1-return-5-4-3-2-1/107676)
- [Why are empty ranges normalized?](https://discourse.julialang.org/t/why-are-empty-ranges-normalized/109836)

---

<div class="post-metadata">

**Author:** ![dylanxyz](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dylanxyz/32/36646_2.png) [@dylanxyz](https://discourse.julialang.org/u/dylanxyz)\
**Post date:** [March 6, 2024, 6:25pm UTC](https://discourse.julialang.org/t/question-about-range-operator/111247/6 "2024-03-06T18:25:43Z")

</div>

You can also do `reverse(1:5)` which i think is more readable than `5:-1:1`.

---

<div class="post-metadata">

**Author:** ![opera\_malenky](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/opera_malenky/32/8213_2.png) [@opera\_malenky](https://discourse.julialang.org/u/opera_malenky)\
**Post date:** [March 6, 2024, 7:10pm UTC](https://discourse.julialang.org/t/question-about-range-operator/111247/7 "2024-03-06T19:10:59Z")

</div>

Ah, I like that one, thanks. 🙂

---

<div class="post-metadata">

**Author:** ![BioTurboNick](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bioturbonick/32/6380_2.png) [@BioTurboNick](https://discourse.julialang.org/u/BioTurboNick)\
**Post date:** [March 6, 2024, 8:20pm UTC](https://discourse.julialang.org/t/question-about-range-operator/111247/8 "2024-03-06T20:20:10Z")

</div>

You know, this is a common enough error that it would probably be worth explicitly calling out in the docstring.

PR: [Explicitly call out reverse ranges in `:` docstring by BioTurboNick · Pull Request #53626 · JuliaLang/julia · GitHub](https://github.com/JuliaLang/julia/pull/53626)
