# Convert AbstractVector to Vector

**URL:** https://discourse.julialang.org/t/convert-abstractvector-to-vector/28098
**Category:** General Usage
**Created:** [August 28, 2019, 11:50am UTC](https://discourse.julialang.org/t/convert-abstractvector-to-vector/28098 "2019-08-28T11:50:20Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![greg\_plowman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/greg_plowman/32/8100_2.png) [@greg\_plowman](https://discourse.julialang.org/u/greg_plowman)
#### Post date: [August 28, 2019, 11:50am UTC](https://discourse.julialang.org/t/convert-abstractvector-to-vector/28098/1 "2019-08-28T11:50:20Z")

</div>

What is a generic, idiomatic and performant method to convert an `AbstractVector` (which may have non-standard indexing) to a `Vector`?  
I want to treat the `AbstractVector` as list of items to be “loaded” into a regular `Vector`.

```julia
MyConvert(A::AbstractArray{T}) where {T} = copy!(Vector{T}(undef, length(A)), A)

```

Will this always work, and be most efficient?

---

<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: [August 28, 2019, 12:20pm UTC](https://discourse.julialang.org/t/convert-abstractvector-to-vector/28098/2 "2019-08-28T12:20:41Z")

</div>

Easier to do `Vector(A)`, for example:

```julia
julia> Vector(1:3)
3-element Array{Int64,1}:
 1
 2
 3

```

But in general I would urge you to avoid this conversion if you can — a different array type could easily be _more_ efficient than a `Vector` for various specialized cases. For example, a range like `1:10^7` requires vastly less memory than a corresponding `Vector`. It is better to write generic code that works with any array type. The compiler will specialize your code for you on the actual argument type that is passed.

---

<div class="post-metadata">

### Author: ![Vasily\_Pisarev](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/vasily_pisarev/32/7929_2.png) [@Vasily\_Pisarev](https://discourse.julialang.org/u/Vasily_Pisarev)
#### Post date: [August 28, 2019, 1:20pm UTC](https://discourse.julialang.org/t/convert-abstractvector-to-vector/28098/3 "2019-08-28T13:20:25Z")

</div>

I prefer `A[:]` or `[A...]` (for an arbitrary iterable object).

---

<div class="post-metadata">

### Author: ![simeonschaub](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/simeonschaub/32/216566_2.png) [@simeonschaub](https://discourse.julialang.org/u/simeonschaub)
#### Post date: [August 28, 2019, 1:23pm UTC](https://discourse.julialang.org/t/convert-abstractvector-to-vector/28098/4 "2019-08-28T13:23:34Z")

</div>

`A[:]` won’t convert to a `Vector`, if `A` is a `UnitRange` for example, and the argument splatting in `[A...]` will be very inefficient for larger sizes.

---

<div class="post-metadata">

### Author: ![greg\_plowman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/greg_plowman/32/8100_2.png) [@greg\_plowman](https://discourse.julialang.org/u/greg_plowman)
#### Post date: [August 28, 2019, 10:34pm UTC](https://discourse.julialang.org/t/convert-abstractvector-to-vector/28098/5 "2019-08-28T22:34:34Z")

</div>

> [@stevengj](#):
>
> Easier to do `Vector(A)`

`Vector(A)` doesn’t seem to work for OffsetArrays:

```julia
Julia-1.2.0> using OffsetArrays

Julia-1.2.0> A = OffsetVector([1,2,3], -1:1)
3-element OffsetArray(::Array{Int64,1}, -1:1) with eltype Int64 with indices -1:1:
 1
 2
 3

Julia-1.2.0> Vector(A)
ERROR: BoundsError: attempt to access 3-element Array{Int64,1} at index [Base.IdentityUnitRange(-1:1)]
Stacktrace:
 [1] throw_boundserror(::Array{Int64,1}, ::Tuple{Base.IdentityUnitRange{UnitRange{Int64}}}) at .\abstractarray.jl:538
 [2] checkbounds at .\abstractarray.jl:503 [inlined]
 [3] copyto!(::Array{Int64,1}, ::OffsetArray{Int64,1,Array{Int64,1}}) at .\multidimensional.jl:902
 [4] Array{T,1} where T(::OffsetArray{Int64,1,Array{Int64,1}}) at .\array.jl:482
 [5] top-level scope at REPL[7]:1

```

whereas explicit copy to Vector does seem work:

```julia
Julia-1.2.0> copy!(Vector{Int}(undef, length(A)), A)
3-element Array{Int64,1}:
 1
 2
 3

```

---

<div class="post-metadata">

### Author: ![greg\_plowman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/greg_plowman/32/8100_2.png) [@greg\_plowman](https://discourse.julialang.org/u/greg_plowman)
#### Post date: [August 28, 2019, 10:41pm UTC](https://discourse.julialang.org/t/convert-abstractvector-to-vector/28098/6 "2019-08-28T22:41:59Z")

</div>

> [@stevengj](#):
>
> But in general I would urge you to avoid this conversion if you can — a different array type could easily be _more_ efficient than a `Vector` for various specialized cases. For example, a range like `1:10^7` requires vastly less memory than a corresponding `Vector` . It is better to write generic code that works with any array type. The compiler will specialize your code for you on the actual argument type that is passed.

Yes, this is a good point. I might have to rethink my implementation.

My usecase is for a “WeightedVector”, which returns values base on weights. In this case, it seemed appropriate for `values` to also be a `Vector` to match `weights`, so that the indices are the same (when `weight[i]` is selected, return `value[i]`).

```julia
struct WeightVector{W<:Real,V}
    weights::Vector{W}
    values::Vector{V}
    sumWeights::W

    function WeightVector{W,V}(weights::Vector{W}, values::Vector{V}) where {W<:Real,V}
        length(weights) == length(values) || throw(ArgumentError("length of weights and values vectors must be equal"))
        issorted(weights, rev=true) || throw(ArgumentError("weights must be sorted in descending order"))
        return new(weights, values, sum(weights))
    end
end

```

I could perhaps change `values::Vector{V}` ----\> `values::A where A<:AbstractVector`

---

<div class="post-metadata">

### Author: ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)
#### Post date: [August 29, 2019, 5:43am UTC](https://discourse.julialang.org/t/convert-abstractvector-to-vector/28098/7 "2019-08-29T05:43:02Z")

</div>

> [@greg\_plowman](#):
>
> In this case, it seemed appropriate for `values` to also be a `Vector` to match `weights` , so that the indices are the same

These two things are not related at all — you can have two `AbstractVectors` with the same indices.

Incidentally, I would just check

```julia
axes(weights, 1) == axes(values, 1)

```

to support [generalized indexing](https://docs.julialang.org/en/v1/devdocs/offset-arrays/#man-custom-indices-1).

Finally, you may be interested in

> **[GitHub - jw3126/ArgCheck.jl: Package for checking function arguments](https://github.com/jw3126/ArgCheck.jl)**
>
> Package for checking function arguments. Contribute to jw3126/ArgCheck.jl development by creating an account on GitHub.
