# Possible bug in prepend!?

**URL:** <https://discourse.julialang.org/t/possible-bug-in-prepend/54917>\
**Category:** New to Julia\
**Created:** [February 9, 2021, 10:56am UTC](https://discourse.julialang.org/t/possible-bug-in-prepend/54917 "2021-02-09T10:56:05Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![askvorts](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/askvorts/32/7120_2.png) [@askvorts](https://discourse.julialang.org/u/askvorts)\
**Post date:** [February 9, 2021, 10:56am UTC](https://discourse.julialang.org/t/possible-bug-in-prepend/54917/1 "2021-02-09T10:56:05Z")

</div>

Came across the following unexpected asymmetric behaviour for push! and prepend!

```julia
B = Vector{Date}()
prepend!(B,Date(2021,2,2)) # Errors

A = Vector{Date}()      
push!(A,Date(2021,2,2)) # OK 

```

But

```julia
B = Vector{Date}()
prepend!(B,[Date(2021,2,2)]) # OK

A = Vector{Date}()
push!(A,[Date(2021,2,2)]) # Errors

```

Also,

```julia
A = Vector{Float64}()       
push!(A,1.0) # OK

B = Vector{Float64}()       
prepend!(B,1.0) # OK

A = Vector{Float64}()       
push!(A,[1.0]) # Errors

B = Vector{Float64}()
prepend!(B,[1.0]) # OK

```

Shouldn’t the behaviour of push! and prepend! be similar in terms of throwing errors?

Also, although the behaviour of push! is consistent across types, it seems that such is not the case for prepend! (`B = Vector{Float64}(); prepend!(B,1.0)` runs fine, but `B = Vector{Date}(); prepend!(B,Date(2021,2,2))` errors, as shown above)

---

<div class="post-metadata">

**Author:** ![fredrikekre](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fredrikekre/32/1688_2.png) [@fredrikekre](https://discourse.julialang.org/u/fredrikekre)\
**Post date:** [February 9, 2021, 11:00am UTC](https://discourse.julialang.org/t/possible-bug-in-prepend/54917/2 "2021-02-09T11:00:41Z")

</div>

You are looking for `pushfirst!`. `prepend!`, like `append!`, expects multiple entries to iterate over and push into the collection.

Examples:

```julia
julia> A = Int[0]
1-element Vector{Int64}:
 0

julia> push!(A, 1)
2-element Vector{Int64}:
 0
 1

julia> pushfirst!(A, -1)
3-element Vector{Int64}:
 -1
  0
  1

julia> append!(A, [2, 3])
5-element Vector{Int64}:
 -1
  0
  1
  2
  3

julia> prepend!(A, [-3, -2])
7-element Vector{Int64}:
 -3
 -2
 -1
  0
  1
  2
  3

```

---

<div class="post-metadata">

**Author:** ![askvorts](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/askvorts/32/7120_2.png) [@askvorts](https://discourse.julialang.org/u/askvorts)\
**Post date:** [February 9, 2021, 11:37am UTC](https://discourse.julialang.org/t/possible-bug-in-prepend/54917/3 "2021-02-09T11:37:19Z")

</div>

> [@fredrikekre](#):
>
> You are looking for `pushfirst!` . `prepend!` , like `append!` , expects multiple entries to iterate over and push into the collection.

Thank you for the clarification. It makes sense. But still should `B = Vector{Float64}(); prepend!(B,1.0)` run fine, and `B = Vector{Date}(); prepend!(B,Date(2021,2,2))` error?

---

<div class="post-metadata">

**Author:** ![rfourquet](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rfourquet/32/3610_2.png) [@rfourquet](https://discourse.julialang.org/u/rfourquet)\
**Post date:** [February 9, 2021, 11:38am UTC](https://discourse.julialang.org/t/possible-bug-in-prepend/54917/4 "2021-02-09T11:38:37Z")

</div>

> [@askvorts](#):
>
> But still should `B = Vector{Float64}(); prepend!(B,1.0)` run fine

That’s because numbers are iterable (they act as a collection with one element).

---

<div class="post-metadata">

**Author:** ![askvorts](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/askvorts/32/7120_2.png) [@askvorts](https://discourse.julialang.org/u/askvorts)\
**Post date:** [February 9, 2021, 11:48am UTC](https://discourse.julialang.org/t/possible-bug-in-prepend/54917/5 "2021-02-09T11:48:55Z")

</div>

I see, interesting. Thank you
