# How to only read (first or last better) element of Set?

**URL:** <https://discourse.julialang.org/t/how-to-only-read-first-or-last-better-element-of-set/35635>\
**Category:** General Usage\
**Created:** [March 6, 2020, 11:30am UTC](https://discourse.julialang.org/t/how-to-only-read-first-or-last-better-element-of-set/35635 "2020-03-06T11:30:35Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![programista](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/programista/32/5372_2.png) [@programista](https://discourse.julialang.org/u/programista)\
**Post date:** [March 6, 2020, 11:30am UTC](https://discourse.julialang.org/t/how-to-only-read-first-or-last-better-element-of-set/35635/1 "2020-03-06T11:30:35Z")

</div>

How to only read (first or last better) element of Set ?

```julia
 julia> X=Set(["a","b"])
Set(["b", "a"])

julia> X[1]
ERROR: MethodError: no method matching getindex(::Set{String}, ::Int64)
Stacktrace:
[1] top-level scope at none:0

julia> X(1)
ERROR: MethodError: objects of type Set{String} are not callable
Stacktrace:
[1] top-level scope at none:0

julia> pop(X)
ERROR: UndefVarError: pop not defined
Stacktrace:
[1] top-level scope at none:0

julia> pop!(X)
"b"
julia> X
Set(["a"])

```

---

<div class="post-metadata">

**Author:** ![nilshg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nilshg/32/2283_2.png) [@nilshg](https://discourse.julialang.org/u/nilshg)\
**Post date:** [March 6, 2020, 11:35am UTC](https://discourse.julialang.org/t/how-to-only-read-first-or-last-better-element-of-set/35635/2 "2020-03-06T11:35:29Z")

</div>

Sets are unordered so what is the first or last element?

---

<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:** [March 6, 2020, 11:35am UTC](https://discourse.julialang.org/t/how-to-only-read-first-or-last-better-element-of-set/35635/3 "2020-03-06T11:35:33Z")

</div>

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

```

This gives the “first element” according to iteration order (which is not insertion order nor sorted order, at least for `Base.Set`).

---

<div class="post-metadata">

**Author:** ![programista](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/programista/32/5372_2.png) [@programista](https://discourse.julialang.org/u/programista)\
**Post date:** [March 6, 2020, 11:43am UTC](https://discourse.julialang.org/t/how-to-only-read-first-or-last-better-element-of-set/35635/4 "2020-03-06T11:43:31Z")

</div>

I mean the one that returns the pop function! . Before pop! I want to know what is there

---

<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:** [March 6, 2020, 11:51am UTC](https://discourse.julialang.org/t/how-to-only-read-first-or-last-better-element-of-set/35635/5 "2020-03-06T11:51:54Z")

</div>

The current implementation of `pop!` and `first` for `Set` make them return the same element, and I don’t think this will change anytime soon. But this is not guarranteed in general for `AbstractSet`.

---

<div class="post-metadata">

**Author:** ![programista](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/programista/32/5372_2.png) [@programista](https://discourse.julialang.org/u/programista)\
**Post date:** [March 6, 2020, 12:20pm UTC](https://discourse.julialang.org/t/how-to-only-read-first-or-last-better-element-of-set/35635/6 "2020-03-06T12:20:29Z")

</div>

What do you think about collect(Set)[1] ?

---

<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:** [March 6, 2020, 12:31pm UTC](https://discourse.julialang.org/t/how-to-only-read-first-or-last-better-element-of-set/35635/7 "2020-03-06T12:31:17Z")

</div>

EDIT: sorry, I didn’t see the `collect` in your example, my answer below is for indexing into a `Set` directly. Using `collect(set)[1]` works but is awfully inefficient, it has to iterate through all the set, just to return the first element.

~~It doesn’t work, and won’t be made to work: the indexing operation is meant to be fast, and a `Set` can’t generally be indexed fast. We can’t support only `s[1]` (for `first`) and not `s[n]` for any `n`.~~

---

<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:** [March 6, 2020, 1:49pm UTC](https://discourse.julialang.org/t/how-to-only-read-first-or-last-better-element-of-set/35635/8 "2020-03-06T13:49:53Z")

</div>

> [@programista](#):
>
> I mean the one that returns the pop function! . Before pop! I want to know what is there

AFAIK there is no exposed API for `Set` that allows this, as it is not an ordered collection. An implementation of `pop!` that picks a random element would be conforming.

It is very likely that you want to use (ie convert to) something else, but without some context it is not possible to give more specific advice.
