# How do I access the elements of a set?

**URL:** <https://discourse.julialang.org/t/how-do-i-access-the-elements-of-a-set/132613>\
**Category:** New to Julia\
**Created:** [September 24, 2025, 6:31am UTC](https://discourse.julialang.org/t/how-do-i-access-the-elements-of-a-set/132613 "2025-09-24T06:31:15Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![hack3rcon](https://avatars.discourse-cdn.com/v4/letter/h/96bed5/32.png) [@hack3rcon](https://discourse.julialang.org/u/hack3rcon)\
**Post date:** [September 24, 2025, 6:31am UTC](https://discourse.julialang.org/t/how-do-i-access-the-elements-of-a-set/132613/1 "2025-09-24T06:31:15Z")

</div>

Hello,  
I defined a `set` as below:

`S = Set([11, 13, 17, 19])`

How can I access the set element:

```julia-auto
julia> S[1]
ERROR: MethodError: no method matching getindex(::Set{Int64}, ::Int64)
Stacktrace:
 [1] top-level scope
   @ REPL[4]:1

julia> S(1)
ERROR: MethodError: objects of type Set{Int64} are not callable
Stacktrace:
 [1] top-level scope
   @ REPL[5]:1

```

Thank you.

---

<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:** [September 24, 2025, 6:45am UTC](https://discourse.julialang.org/t/how-do-i-access-the-elements-of-a-set/132613/2 "2025-09-24T06:45:26Z")

</div>

> [@hack3rcon](#):
>
> How can I access the set element:

Sets aren’t ordered, so there isn’t a well-defined meaning of `S[n]`.

You can still iterate over a `Set`, in which Julia picks some arbitrary order, e.g.

```julia-auto
julia> for x in S
           @show x
       end
x = 13
x = 11
x = 17
x = 19

```

and you can convert it into an indexable array in this order with `collect`:

```julia-auto
julia> a = collect(S)
4-element Vector{Int64}:
 13
 11
 17
 19

julia> a[1]
13

```

However, the fact that you want `S[1]` strongly suggests that you may be using the wrong data structure for your problem.

The key thing that a `Set` is good at is testing inclusion:

```julia-auto
julia> 11 in S
true

julia> 12 in S
false

```

Why are you using a `Set`? What operations do you want to perform on your data?

---

<div class="post-metadata">

**Author:** ![hack3rcon](https://avatars.discourse-cdn.com/v4/letter/h/96bed5/32.png) [@hack3rcon](https://discourse.julialang.org/u/hack3rcon)\
**Post date:** [September 24, 2025, 6:50am UTC](https://discourse.julialang.org/t/how-do-i-access-the-elements-of-a-set/132613/3 "2025-09-24T06:50:43Z")

</div>

Hello,  
Thank you so much for your reply.  
I am a beginner and learning Julia.

---

<div class="post-metadata">

**Author:** ![WalterMadelim](https://avatars.discourse-cdn.com/v4/letter/w/3e96dc/32.png) [@WalterMadelim](https://discourse.julialang.org/u/WalterMadelim)\
**Post date:** [September 24, 2025, 7:44am UTC](https://discourse.julialang.org/t/how-do-i-access-the-elements-of-a-set/132613/4 "2025-09-24T07:44:03Z")

</div>

If you just want to draw a (representative) element, you can use `first(S)` though (but no `last`).
