# One-dimensional Array access

**URL:** <https://discourse.julialang.org/t/one-dimensional-array-access/40427>\
**Category:** New to Julia\
**Created:** [May 29, 2020, 4:21pm UTC](https://discourse.julialang.org/t/one-dimensional-array-access/40427 "2020-05-29T16:21:19Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![erlebach](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/erlebach/32/12973_2.png) [@erlebach](https://discourse.julialang.org/u/erlebach)\
**Post date:** [May 29, 2020, 4:21pm UTC](https://discourse.julialang.org/t/one-dimensional-array-access/40427/1 "2020-05-29T16:21:19Z")

</div>

I have an array:

```julia
a = collect(1:20)

```

and I wish to access elements 1:10 and 14. How is that done?  
In python, with Numpy, I could do `a[1:10,14]`. Is there an accepted approach in Julia. Everything I tried has failed. I am also wanting to access non-contiguous columns in a Dataframe with 30 columns, say, columns 10:15 and 20:25.

Thanks, Gordon.

---

<div class="post-metadata">

**Author:** ![laborg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/laborg/32/5474_2.png) [@laborg](https://discourse.julialang.org/u/laborg)\
**Post date:** [May 29, 2020, 4:28pm UTC](https://discourse.julialang.org/t/one-dimensional-array-access/40427/2 "2020-05-29T16:28:54Z")

</div>

Hi!  
This should work

```julia
a[[1:10...,14]]

```

and for the DataFrames:

```julia
select(df,10:15...,20:25...)

```

---

<div class="post-metadata">

**Author:** ![erlebach](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/erlebach/32/12973_2.png) [@erlebach](https://discourse.julialang.org/u/erlebach)\
**Post date:** [May 29, 2020, 4:43pm UTC](https://discourse.julialang.org/t/one-dimensional-array-access/40427/3 "2020-05-29T16:43:41Z")

</div>

I learned something new!! I thought that … means any number of what preceded it.  
What is the rationale for the use of the triple-dot in this way?

I solved my problem with:

```julia
@df df_ode plot(:t,

cols([2:6...,1]), # as an example

label=["S1" "S2" "S3" "S4" "S5" "S6"],
xlabel="Time",
ylabel="Number")

```

---

<div class="post-metadata">

**Author:** ![rdeits](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rdeits/32/286_2.png) [@rdeits](https://discourse.julialang.org/u/rdeits)\
**Post date:** [May 29, 2020, 4:48pm UTC](https://discourse.julialang.org/t/one-dimensional-array-access/40427/4 "2020-05-29T16:48:07Z")

</div>

> [@erlebach](#):
>
> In python, with Numpy, I could do `a[1:10,14]`

By the way, you actually can’t do that:

```python
In [16]: a = np.array(range(20))                                                                                                                                                                                                           

In [17]: a[1:10, 14]                                                                                                                                                                                                                       
---------------------------------------------------------------------------
IndexError Traceback (most recent call last)
<ipython-input-17-0de60f11d6ed> in <module>
----> 1 a[1:10, 14]

IndexError: too many indices for array

In [18]: a[range(0, 10), 14]                                                                                                                                                                                                               
---------------------------------------------------------------------------
IndexError Traceback (most recent call last)
<ipython-input-18-0c6926e5d9b3> in <module>
----> 1 a[range(0, 10), 14]

IndexError: too many indices for array

```

The thing that actually works for me in Python is also what I would recommend in Julia:

```julia
In [19]: a[np.hstack((range(0, 10), 14))]                                                                           
Out[19]: array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 14])

```

```julia
julia> a = 1:20
1:20

julia> a[vcat(1:10, 14)]
11-element Array{Int64,1}:
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 14

```

---

<div class="post-metadata">

**Author:** ![rdeits](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rdeits/32/286_2.png) [@rdeits](https://discourse.julialang.org/u/rdeits)\
**Post date:** [May 29, 2020, 4:53pm UTC](https://discourse.julialang.org/t/one-dimensional-array-access/40427/5 "2020-05-29T16:53:20Z")

</div>

> [@erlebach](#):
>
> What is the rationale for the use of the triple-dot in this way?

`...` is sort of its own inverse operation:

- In a function signature, `f(args...)` accepts any number of function arguments, collecting them as a tuple named `args`.
- When used elsewhere, `args...` takes a tuple (or actually any iterable at all) and expands it to the same number of positional arguments.

Here’s an example of doing both:

```julia
julia> function f(args...)
         @show args
         +(args...)
       end
f (generic function with 1 method)

julia> f(1, 2)
args = (1, 2)
3

julia> f(1, 2, 3)
args = (1, 2, 3)
6

```

The function `f` takes any number of arguments. It receives them as a tuple named `args`. It then hands that tuple as multiple arguments to the function `+` (which also happens to take multiple arguments).

This is exactly the same as `*` in Python:

```julia
In [25]: def f(*args): 
    ...: print("args:", args) 
    ...: return sum([*args]) 
    ...:                                                                                                            

In [26]: f(1, 2)                                                                                                    
args: (1, 2)
Out[26]: 3

In [27]: f(1, 2, 3)                                                                                                 
args: (1, 2, 3)
Out[27]: 6

```

---

<div class="post-metadata">

**Author:** ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)\
**Post date:** [May 29, 2020, 4:59pm UTC](https://discourse.julialang.org/t/one-dimensional-array-access/40427/6 "2020-05-29T16:59:29Z")

</div>

> [@rdeits](#):
>
> `a[vcat(1:10, 14)]`

A nicer syntax for this is

```julia
a[[1:10; 14]] 

```

---

<div class="post-metadata">

**Author:** ![erlebach](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/erlebach/32/12973_2.png) [@erlebach](https://discourse.julialang.org/u/erlebach)\
**Post date:** [May 29, 2020, 5:02pm UTC](https://discourse.julialang.org/t/one-dimensional-array-access/40427/7 "2020-05-29T17:02:29Z")

</div>

Thank you everybody!! I really thought this would work in Python, but I stand corrected.  
I guess the issue was mixing slices with non-consecutive indexes. I appreciate the help

Gordon

---

<div class="post-metadata">

**Author:** ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)\
**Post date:** [May 29, 2020, 5:14pm UTC](https://discourse.julialang.org/t/one-dimensional-array-access/40427/8 "2020-05-29T17:14:43Z")

</div>

> [@erlebach](#):
>
> I guess the issue was mixing slices with non-consecutive indexes. I appreciate the help

Depends what you mean, but it wasn’t the mixing, as such. You would have the same problem with

```julia
a[1:10, 11:20]

```

The issue is that you have to supply a _single_ vector argument as index. You provided a list of _two_ input arguments.

---

<div class="post-metadata">

**Author:** ![Skoffer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/skoffer/32/378_2.png) [@Skoffer](https://discourse.julialang.org/u/Skoffer)\
**Post date:** [May 29, 2020, 5:20pm UTC](https://discourse.julialang.org/t/one-dimensional-array-access/40427/9 "2020-05-29T17:20:57Z")

</div>

For accessing DataFrames columns you can also use various combinations of `Regex`, `All` , `Between` and `Not`. You can find examples of usage in [DataFrames Tutorial](https://github.com/bkamins/Julia-DataFrames-Tutorial), specifically [working with columns](https://github.com/bkamins/Julia-DataFrames-Tutorial/blob/master/05_columns.ipynb) notebook.

---

<div class="post-metadata">

**Author:** ![erlebach](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/erlebach/32/12973_2.png) [@erlebach](https://discourse.julialang.org/u/erlebach)\
**Post date:** [May 29, 2020, 5:50pm UTC](https://discourse.julialang.org/t/one-dimensional-array-access/40427/10 "2020-05-29T17:50:22Z")

</div>

I understand, and it makes sense.
