# How to call an element of a set?

**URL:** https://discourse.julialang.org/t/how-to-call-an-element-of-a-set/68648
**Category:** New to Julia
**Tags:** question, arrays, sets
**Created:** [September 23, 2021, 8:17pm UTC](https://discourse.julialang.org/t/how-to-call-an-element-of-a-set/68648 "2021-09-23T20:17:48Z")
**Posts on this page:** 10
**Page:** 1

<div class="post-metadata">

### Author: ![leon](https://avatars.discourse-cdn.com/v4/letter/l/dc4da7/32.png) [@leon](https://discourse.julialang.org/u/leon)
#### Post date: [September 23, 2021, 8:17pm UTC](https://discourse.julialang.org/t/how-to-call-an-element-of-a-set/68648/1 "2021-09-23T20:17:48Z")

</div>

Sorry for the very silly question. I’m trying to figure out how to call the elements of a set. For example in my below example, how do I get the first element, i.e., “Acg”?

A = Set([“Acg”, “Bdc”, “HH”]);

I tried, A[1], A(1), A{1}, but none of them worked.

---

<div class="post-metadata">

### Author: ![Sukera](https://avatars.discourse-cdn.com/v4/letter/s/ce7236/32.png) [@Sukera](https://discourse.julialang.org/u/Sukera)
#### Post date: [September 23, 2021, 8:19pm UTC](https://discourse.julialang.org/t/how-to-call-an-element-of-a-set/68648/2 "2021-09-23T20:19:41Z")

</div>

The default `Set` in `Base` is not ordered - there is no “first” element. You may want to check out `OrderedSet` from `DataStructures.jl` instead, though usually depending on a specific ordering could also be served by using `unique` on a regular vector.

---

<div class="post-metadata">

### Author: ![leon](https://avatars.discourse-cdn.com/v4/letter/l/dc4da7/32.png) [@leon](https://discourse.julialang.org/u/leon)
#### Post date: [September 23, 2021, 8:28pm UTC](https://discourse.julialang.org/t/how-to-call-an-element-of-a-set/68648/3 "2021-09-23T20:28:41Z")

</div>

I see.

Many thanks. In this case, it seems that Julia is trying to make something really simple really hard 🙂

---

<div class="post-metadata">

### Author: ![Sukera](https://avatars.discourse-cdn.com/v4/letter/s/ce7236/32.png) [@Sukera](https://discourse.julialang.org/u/Sukera)
#### Post date: [September 23, 2021, 8:31pm UTC](https://discourse.julialang.org/t/how-to-call-an-element-of-a-set/68648/4 "2021-09-23T20:31:36Z")

</div>

It’s a design choice like any other - the common case for a `Set` is to be unordered, it just happens that some languages like python leak some internal ordering or even guarantee an order, locking them into that decision for backwards compatibility reasons. If the default here is unordered, julia is free to optimize the internal datastructure as much as we want, changing the order to provide more efficient storage etc.

Sure it has its pros and cons, but to me that is a weird thing to be split on. It may also be worthwhile to think about why you need a specific ordering of a `Set` in the first place, and if a different datastructure may be better suited to the task at hand.

---

<div class="post-metadata">

### Author: ![jzr](https://avatars.discourse-cdn.com/v4/letter/j/eb9ed0/32.png) [@jzr](https://discourse.julialang.org/u/jzr)
#### Post date: [September 23, 2021, 9:00pm UTC](https://discourse.julialang.org/t/how-to-call-an-element-of-a-set/68648/5 "2021-09-23T21:00:44Z")

</div>

> [@Sukera](#):
>
> some languages like python leak some internal ordering or even guarantee an order, locking them into that decision for backwards compatibility reasons.

Python doesn’t guarantee a set order and Julia leaks the internal order just as much as Python does, e.g. with `first(s)`.

---

<div class="post-metadata">

### Author: ![PeterSimon](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/petersimon/32/25193_2.png) [@PeterSimon](https://discourse.julialang.org/u/PeterSimon)
#### Post date: [September 24, 2021, 12:29am UTC](https://discourse.julialang.org/t/how-to-call-an-element-of-a-set/68648/8 "2021-09-24T00:29:18Z")

</div>

Are you aware that you can use an array for this purpose? It is designed to retain the ordering of it’s elements:

```julia
A = ["Acg", "Bdc", "HH"]
A[1]

```

---

<div class="post-metadata">

### Author: ![leon](https://avatars.discourse-cdn.com/v4/letter/l/dc4da7/32.png) [@leon](https://discourse.julialang.org/u/leon)
#### Post date: [September 24, 2021, 12:34am UTC](https://discourse.julialang.org/t/how-to-call-an-element-of-a-set/68648/9 "2021-09-24T00:34:08Z")

</div>

Many thanks! That will do the trick.

---

<div class="post-metadata">

### Author: ![leon](https://avatars.discourse-cdn.com/v4/letter/l/dc4da7/32.png) [@leon](https://discourse.julialang.org/u/leon)
#### Post date: [September 24, 2021, 12:35am UTC](https://discourse.julialang.org/t/how-to-call-an-element-of-a-set/68648/10 "2021-09-24T00:35:32Z")

</div>

This is probably the best solution. I wasn’t aware of it, because I’m a brand new Julia user. Many thanks!

---

<div class="post-metadata">

### Author: ![jling](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jling/32/212909_2.png) [@jling](https://discourse.julialang.org/u/jling)
#### Post date: [September 24, 2021, 1:09am UTC](https://discourse.julialang.org/t/how-to-call-an-element-of-a-set/68648/11 "2021-09-24T01:09:30Z")

</div>

A `Set` has a pretty mathematical meaning (i.e. unordered), it’s not just “a few items laid out in this way”.

---

<div class="post-metadata">

### Author: ![jzr](https://avatars.discourse-cdn.com/v4/letter/j/eb9ed0/32.png) [@jzr](https://discourse.julialang.org/u/jzr)
#### Post date: [September 24, 2021, 1:38am UTC](https://discourse.julialang.org/t/how-to-call-an-element-of-a-set/68648/12 "2021-09-24T01:38:57Z")

</div>

Set also has no duplicates and different performance characteristics.
