# Is there a Julia equivalent for python's .remove() function?

**URL:** <https://discourse.julialang.org/t/is-there-a-julia-equivalent-for-pythons-remove-function/33031>\
**Category:** New to Julia\
**Tags:** question\
**Created:** [January 6, 2020, 4:07pm UTC](https://discourse.julialang.org/t/is-there-a-julia-equivalent-for-pythons-remove-function/33031 "2020-01-06T16:07:24Z")\
**Posts on this page:** 15\
**Page:** 1

<div class="post-metadata">

**Author:** ![aschimmichanga](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/aschimmichanga/32/11998_2.png) [@aschimmichanga](https://discourse.julialang.org/u/aschimmichanga)\
**Post date:** [January 6, 2020, 4:07pm UTC](https://discourse.julialang.org/t/is-there-a-julia-equivalent-for-pythons-remove-function/33031/1 "2020-01-06T16:07:24Z")

</div>

I have tried deleteat!(arr, findfirst(item,arr)) but it gives me an error. Is it deprecated?

---

<div class="post-metadata">

**Author:** ![affans](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/affans/32/11911_2.png) [@affans](https://discourse.julialang.org/u/affans)\
**Post date:** [January 6, 2020, 4:10pm UTC](https://discourse.julialang.org/t/is-there-a-julia-equivalent-for-pythons-remove-function/33031/2 "2020-01-06T16:10:28Z")

</div>

You can try `popfirst!` if you just want to remove the first element of the array.

---

<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:** [January 6, 2020, 4:14pm UTC](https://discourse.julialang.org/t/is-there-a-julia-equivalent-for-pythons-remove-function/33031/3 "2020-01-06T16:14:13Z")

</div>

You probably want:

```julia
findfirst(isequal(item), arr)

```

to get the index of the first item which is equal to `item`.

This behavior changed in Julia 1.0, so if you have other old code you might find that running it in Julia 0.7 is helpful, since it will tell you exactly what changed and how to fix your code:

```julia
julia-0.7> findfirst(1, [1, 2, 3])
┌ Warning: `findfirst(A, v)` is deprecated, use `something(findfirst(isequal(v), A), 0)` instead.
│ caller = top-level scope at none:0
└ @ Core none:0
0

```

---

<div class="post-metadata">

**Author:** ![aschimmichanga](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/aschimmichanga/32/11998_2.png) [@aschimmichanga](https://discourse.julialang.org/u/aschimmichanga)\
**Post date:** [January 6, 2020, 5:35pm UTC](https://discourse.julialang.org/t/is-there-a-julia-equivalent-for-pythons-remove-function/33031/6 "2020-01-06T17:35:27Z")

</div>

`findfirst(isequal(item), arr)` gives me the error `LoadError: MethodError: no method matching iterate(::Nothing)`

Here is the block of code where it is for some context:

```julia
coord = [(i, j) for i = 1:3 for j = 1:3]
corners = [(i, j) for i = 1:2:3 for j = 1:2:3]
for i = 1:3
    for j = 1:3
        (i, j) in coord ? deleteat!(corners, findfirst(isequal((i, j)), corners)) :
        push!(coord, (i, j))
    end
end

```

It’s not the completed code, but this is the general part that the issue is in…

Also how would you run julia 0.7 in the REPL?

---

<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:** [January 6, 2020, 5:43pm UTC](https://discourse.julialang.org/t/is-there-a-julia-equivalent-for-pythons-remove-function/33031/7 "2020-01-06T17:43:10Z")

</div>

> [@aschimmichanga](#):
>
> `findfirst(isequal(item), arr)` gives me the error `LoadError: MethodError: no method matching iterate(::Nothing)`

Presumably this error happens when you call `deleteat!` with that result? It’s much easier to help if you can provide a way to actually reproduce the error and/or a complete backtrace.

I assume that what’s happening is that `arr` does not contain `item`. In that case, `findfirst` returns `nothing`:

```julia
julia> arr = [1, 2, 3]
3-element Array{Int64,1}:
 1
 2
 3

julia> findfirst(isequal(4), arr)

julia> findfirst(isequal(4), arr) === nothing
true

```

And `deleteat!` does not accept `nothing` (unsurprisingly):

```julia
julia> deleteat!(arr, findfirst(isequal(4), arr))
ERROR: MethodError: no method matching iterate(::Nothing)  

```

Note that you get an error in python as well:

```julia
In [1]: [1, 2, 3].remove(5)
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-1-c5f0d54cbcf3> in <module>()
----> 1 [1, 2, 3].remove(5)

ValueError: list.remove(x): x not in list

```

although admittedly it’s more obvious.

There may not be a built-in function that does exactly what you want in this case. You might need to write one:

```julia
function remove!(collection::AbstractVector, item)
  index = findfirst(isequal(item), collection)
  if index === nothing
    error("$item is not in $collection")
  else
    deleteat!(collection, index)
  end
end

```

```julia
julia> arr = [1, 2, 3]
3-element Array{Int64,1}:
 1
 2
 3

julia> remove!(arr, 1)
2-element Array{Int64,1}:
 2
 3

julia> arr
2-element Array{Int64,1}:
 2
 3

julia> remove!(arr, 4)
ERROR: 4 is not in [2, 3]

```

---

<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:** [January 6, 2020, 5:46pm UTC](https://discourse.julialang.org/t/is-there-a-julia-equivalent-for-pythons-remove-function/33031/8 "2020-01-06T17:46:15Z")

</div>

> [@aschimmichanga](#):
>
> Also how would you run julia 0.7 in the REPL?

To run a new Julia version, you need to download it from [https://julialang.org/downloads/oldreleases.html](https://julialang.org/downloads/oldreleases.html) and then just ensure it’s available on your path. I wrote up my personal workflow for dealing with multiple Julia versions here: [Transitioning from Ubuntu PPA to official binaries - #2 by rdeits](https://discourse.julialang.org/t/transitioning-from-ubuntu-ppa-to-official-binaries/28650/2) and I’ve been pretty happy with it.

---

<div class="post-metadata">

**Author:** ![dpsanders](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dpsanders/32/3573_2.png) [@dpsanders](https://discourse.julialang.org/u/dpsanders)\
**Post date:** [January 6, 2020, 6:10pm UTC](https://discourse.julialang.org/t/is-there-a-julia-equivalent-for-pythons-remove-function/33031/10 "2020-01-06T18:10:11Z")

</div>

Isn’t this the function `delete! `?

---

<div class="post-metadata">

**Author:** ![aschimmichanga](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/aschimmichanga/32/11998_2.png) [@aschimmichanga](https://discourse.julialang.org/u/aschimmichanga)\
**Post date:** [January 6, 2020, 6:17pm UTC](https://discourse.julialang.org/t/is-there-a-julia-equivalent-for-pythons-remove-function/33031/11 "2020-01-06T18:17:32Z")

</div>

This gives the error `MethodError: no method matching findfirst(::Bool, ::Array{Tuple{Int64,Int64},2})`

---

<div class="post-metadata">

**Author:** ![aschimmichanga](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/aschimmichanga/32/11998_2.png) [@aschimmichanga](https://discourse.julialang.org/u/aschimmichanga)\
**Post date:** [January 6, 2020, 6:20pm UTC](https://discourse.julialang.org/t/is-there-a-julia-equivalent-for-pythons-remove-function/33031/12 "2020-01-06T18:20:58Z")

</div>

Does `delete!` work with arrays? It gives me an error and the docs only mentions dictionaries/idsets

---

<div class="post-metadata">

**Author:** ![dpsanders](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dpsanders/32/3573_2.png) [@dpsanders](https://discourse.julialang.org/u/dpsanders)\
**Post date:** [January 6, 2020, 6:27pm UTC](https://discourse.julialang.org/t/is-there-a-julia-equivalent-for-pythons-remove-function/33031/13 "2020-01-06T18:27:15Z")

</div>

Ah no, apparently not, as `methods(delete!)` shows. (I wasn’t at my computer.)

---

<div class="post-metadata">

**Author:** ![dpsanders](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dpsanders/32/3573_2.png) [@dpsanders](https://discourse.julialang.org/u/dpsanders)\
**Post date:** [January 6, 2020, 6:28pm UTC](https://discourse.julialang.org/t/is-there-a-julia-equivalent-for-pythons-remove-function/33031/14 "2020-01-06T18:28:02Z")

</div>

You need to post a minimal working example to get better help: exactly what input and what output do you need?

---

<div class="post-metadata">

**Author:** ![aschimmichanga](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/aschimmichanga/32/11998_2.png) [@aschimmichanga](https://discourse.julialang.org/u/aschimmichanga)\
**Post date:** [January 6, 2020, 6:34pm UTC](https://discourse.julialang.org/t/is-there-a-julia-equivalent-for-pythons-remove-function/33031/15 "2020-01-06T18:34:09Z")

</div>

@dpsanders  
Here is one instance of when I want to use it. I am doing a tic tac toe program and I want to iterate through the corners

```julia
# Corner check
  corners = [(i, j) for i = 1:2:3 for j = 1:2:3]
  while length(corners) > 1
    corner_choice = corners[rand(1:length(corners))]
    if corner_choice in coord
      board[corner_choice[1], corner_choice[2]] = current_player
      return
    else
      deleteat!(corners, findfirst(isequal(corner_choice), corners))
    end
  end

```

---

<div class="post-metadata">

**Author:** ![dpsanders](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dpsanders/32/3573_2.png) [@dpsanders](https://discourse.julialang.org/u/dpsanders)\
**Post date:** [January 6, 2020, 6:53pm UTC](https://discourse.julialang.org/t/is-there-a-julia-equivalent-for-pythons-remove-function/33031/16 "2020-01-06T18:53:59Z")

</div>

When you make the choice of a random element in the list, you are choosing the index. Just store that and use it.

---

<div class="post-metadata">

**Author:** ![aschimmichanga](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/aschimmichanga/32/11998_2.png) [@aschimmichanga](https://discourse.julialang.org/u/aschimmichanga)\
**Post date:** [January 6, 2020, 7:25pm UTC](https://discourse.julialang.org/t/is-there-a-julia-equivalent-for-pythons-remove-function/33031/17 "2020-01-06T19:25:21Z")

</div>

@dpsanders  
That approach wouldn’t work for this part though… (checking for spaces and updating a separate array with coordinates)

```julia
for i = 1:3
    for j = 1:3
      if board[i, j] == " "
        if (i, j) in coord
          deleteat!(coord, findfirst(isequal(i, j), coord))
        else
          push!(coord, (i, j))
        end
      end
    end
  end

```

---

<div class="post-metadata">

**Author:** ![aschimmichanga](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/aschimmichanga/32/11998_2.png) [@aschimmichanga](https://discourse.julialang.org/u/aschimmichanga)\
**Post date:** [January 7, 2020, 6:26pm UTC](https://discourse.julialang.org/t/is-there-a-julia-equivalent-for-pythons-remove-function/33031/18 "2020-01-07T18:26:42Z")

</div>

I ended up using a work around by iterating with a `pairs()` function and generating the tuple coordinate with math.
