# Extract single element from container

**URL:** https://discourse.julialang.org/t/extract-single-element-from-container/9200
**Category:** General Usage
**Tags:** question
**Created:** [February 20, 2018, 12:53pm UTC](https://discourse.julialang.org/t/extract-single-element-from-container/9200 "2018-02-20T12:53:18Z")
**Posts on this page:** 8
**Page:** 1

<div class="post-metadata">

### Author: ![jw3126](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jw3126/32/3086_2.png) [@jw3126](https://discourse.julialang.org/u/jw3126)
#### Post date: [February 20, 2018, 12:53pm UTC](https://discourse.julialang.org/t/extract-single-element-from-container/9200/1 "2018-02-20T12:53:19Z")

</div>

Every now and then I have a container like `[1]` or `Set(["foo"])`, which contains only a single element and I want to extract it. Is there a function that does this somewhere? It should

1. Throw an error, if the container is empty or has more then one element.
2. only assume the iteration protocol, e.g. not use `length`.

If there is nothing official, the following does the trick, what would be a good place to add a PR for it?

```julia
function single(iter)
    s1 = start(iter)
    if done(iter, s1)
        throw(ArgumentError("iter $iter is empty."))
    end
    item, s2 = next(iter, s1)
    
    if !done(iter, s2) 
        throw(ArgumentError("iter $iter has more then one element."))
    end
    item
end

```

---

<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: [February 21, 2018, 10:30am UTC](https://discourse.julialang.org/t/extract-single-element-from-container/9200/2 "2018-02-21T10:30:47Z")

</div>

```julia
julia> first(Set([1]))
1

julia> first([1])
1

julia> first([])
ERROR: BoundsError: attempt to access 0-element Array{Any,1} at index [1]
Stacktrace:
 [1] first(::Array{Any,1}) at ./abstractarray.jl:135

julia> first(Set())
ERROR: ArgumentError: collection must be non-empty
Stacktrace:
 [1] first(::Set{Any}) at ./abstractarray.jl:153

```

does the extraction and checking for emptyness, but will not throw an error if you have multiple elements.

---

<div class="post-metadata">

### Author: ![kristoffer.carlsson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kristoffer.carlsson/32/22_2.png) [@kristoffer.carlsson](https://discourse.julialang.org/u/kristoffer.carlsson)
#### Post date: [February 21, 2018, 10:55am UTC](https://discourse.julialang.org/t/extract-single-element-from-container/9200/3 "2018-02-21T10:55:15Z")

</div>

I can’t find it now but I know there’s been an issue with this exact feature request.

---

<div class="post-metadata">

### Author: ![jw3126](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jw3126/32/3086_2.png) [@jw3126](https://discourse.julialang.org/u/jw3126)
#### Post date: [February 21, 2018, 11:16am UTC](https://discourse.julialang.org/t/extract-single-element-from-container/9200/4 "2018-02-21T11:16:31Z")

</div>

Yeah I often use

```julia
single(iter) = (@assert length(iter) ==1; first(iter))

```

One common case, where this often fails due to lack of length is:

```julia
single(filter(ishighlander, iter))

```

---

<div class="post-metadata">

### Author: ![jw3126](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jw3126/32/3086_2.png) [@jw3126](https://discourse.julialang.org/u/jw3126)
#### Post date: [February 21, 2018, 11:17am UTC](https://discourse.julialang.org/t/extract-single-element-from-container/9200/5 "2018-02-21T11:17:36Z")

</div>

Do you remember something that makes the search easier? Repos? proposed name?..

---

<div class="post-metadata">

### Author: ![kristoffer.carlsson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kristoffer.carlsson/32/22_2.png) [@kristoffer.carlsson](https://discourse.julialang.org/u/kristoffer.carlsson)
#### Post date: [February 21, 2018, 12:49pm UTC](https://discourse.julialang.org/t/extract-single-element-from-container/9200/6 "2018-02-21T12:49:08Z")

</div>

Found it [https://github.com/JuliaLang/julia/pull/25078](https://github.com/JuliaLang/julia/pull/25078). It was a PR.

---

<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: [February 21, 2018, 1:21pm UTC](https://discourse.julialang.org/t/extract-single-element-from-container/9200/7 "2018-02-21T13:21:02Z")

</div>

This looks like something that could live in a package in the meantime.

---

<div class="post-metadata">

### Author: ![andyferris](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/andyferris/32/235_2.png) [@andyferris](https://discourse.julialang.org/u/andyferris)
#### Post date: [February 22, 2018, 3:09am UTC](https://discourse.julialang.org/t/extract-single-element-from-container/9200/8 "2018-02-22T03:09:56Z")

</div>

Yes, you can find the `only` function in SplitApplyCombine.jl but I haven’t got around to publishing that to METADATA yet.
