# Julia equivalent of the Python \`slice\` class

**URL:** <https://discourse.julialang.org/t/julia-equivalent-of-the-python-slice-class/52152>\
**Category:** General Usage\
**Tags:** indexing, python\
**Created:** [December 21, 2020, 1:40am UTC](https://discourse.julialang.org/t/julia-equivalent-of-the-python-slice-class/52152 "2020-12-21T01:40:38Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Krastanov](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/krastanov/32/6817_2.png) [@Krastanov](https://discourse.julialang.org/u/Krastanov)\
**Post date:** [December 21, 2020, 1:40am UTC](https://discourse.julialang.org/t/julia-equivalent-of-the-python-slice-class/52152/1 "2020-12-21T01:40:38Z")

</div>

In Python I can store an index range into a variable and reuse it. **In particular, I can store a range with implicit begin-at-0 and end-at-end-of-list extremas**.

How do I do the following in Julia:

```python
my_list = [1,2,3,4,5,6,7,8,9,0]
my_boring_slice = slice(2,8,3)
my_interesting_slice = slice(3,None,None)
some_sublist = my_list[my_boring_slice]
the_sublist_I_really_want = my_list[my_interesting_slice]

```

I know I can define my own slice type, and I think I understand what methods I need to define in order for it to work on most `Array` types. But I am hopeful there is some obvious class in Base that already implements this. A cursory look through the docs and some googling/ducking did not really help.

---

<div class="post-metadata">

**Author:** ![mbauman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mbauman/32/31082_2.png) [@mbauman](https://discourse.julialang.org/u/mbauman)\
**Post date:** [December 21, 2020, 2:24am UTC](https://discourse.julialang.org/t/julia-equivalent-of-the-python-slice-class/52152/2 "2020-12-21T02:24:22Z")

</div>

Just `my_list[4:end]`? Ok, I get you want to pull out the `4:end` to re-use it, but note that there is _zero_ cost to constructing that thing. Unlike Python, our `:` range syntax works outside of indexing. You can just do `my_boring_slice = 3:3:8` or `my_interesting_slice = 4:length(my_list)` or even `my_whole_dimension = (:)`.

Julia’s slices are just arrays of integers. That’s it. There is a very simple package that you can use — [EndpointRanges.jl](https://github.com/JuliaArrays/EndpointRanges.jl) — that can give you an `end` that works outside of indexing and dynamically matches the arrays it indexes into.

---

<div class="post-metadata">

**Author:** ![Krastanov](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/krastanov/32/6817_2.png) [@Krastanov](https://discourse.julialang.org/u/Krastanov)\
**Post date:** [December 21, 2020, 2:33am UTC](https://discourse.julialang.org/t/julia-equivalent-of-the-python-slice-class/52152/3 "2020-12-21T02:33:05Z")

</div>

Thanks, EndpointRanges is exactly what I needed! I do have a vague sense of discomfort that this is not in Base, but that is probably just because I was burnt by Javascript.

---

<div class="post-metadata">

**Author:** ![Daniel\_Berge](https://avatars.discourse-cdn.com/v4/letter/d/eb9ed0/32.png) [@Daniel\_Berge](https://discourse.julialang.org/u/Daniel_Berge)\
**Post date:** [December 21, 2020, 3:04am UTC](https://discourse.julialang.org/t/julia-equivalent-of-the-python-slice-class/52152/4 "2020-12-21T03:04:19Z")

</div>

Is `EndpointRanges.jl` still necessary? You can also index from the beginning of the array with `begin`, for use with `OffsetArrays.jl` and such.

```julia
using OffsetArrays

a=OffsetArray([1:11;],-6)
#11-element OffsetArray(::Vector{Int64}, -5:5) with eltype Int64 with indices -5:5:

a[3] #9 
a[begin+2:3:end] # [3,6,9]

```

---

<div class="post-metadata">

**Author:** ![Krastanov](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/krastanov/32/6817_2.png) [@Krastanov](https://discourse.julialang.org/u/Krastanov)\
**Post date:** [December 21, 2020, 3:10am UTC](https://discourse.julialang.org/t/julia-equivalent-of-the-python-slice-class/52152/5 "2020-12-21T03:10:02Z")

</div>

I can not store `begin+2:end` in a variable. I specifically need a slice object to be stored in a variable, generated programmatically or modified.
