# How to return the value and the weight of sampled element?

**URL:** <https://discourse.julialang.org/t/how-to-return-the-value-and-the-weight-of-sampled-element/36992>\
**Category:** General Usage\
**Tags:** indexing\
**Created:** [April 3, 2020, 10:51pm UTC](https://discourse.julialang.org/t/how-to-return-the-value-and-the-weight-of-sampled-element/36992 "2020-04-03T22:51:03Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![sergevic](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sergevic/32/9958_2.png) [@sergevic](https://discourse.julialang.org/u/sergevic)\
**Post date:** [April 3, 2020, 10:51pm UTC](https://discourse.julialang.org/t/how-to-return-the-value-and-the-weight-of-sampled-element/36992/1 "2020-04-03T22:51:03Z")

</div>

I want to sample 1 element from `values` with specified `weights`:

```julia
values=[1,3,6,7,9]
weights=[0.1,0,4,0.3,0.1,0.1] 

```

I need to have access to the element sampled but also to its weight in `weights`. With the command

```julia
using StatsBase
sample(values,Weights(weights))

```

i only get the element sampled. If for example `7` is sampled, is it possible to have a result like `(7,0.1)`, where `0.1` is the weight of `7` in `weights`? Thank you!

---

<div class="post-metadata">

**Author:** ![anon94023334](https://avatars.discourse-cdn.com/v4/letter/a/e274bd/32.png) [@anon94023334](https://discourse.julialang.org/u/anon94023334)\
**Post date:** [April 3, 2020, 11:00pm UTC](https://discourse.julialang.org/t/how-to-return-the-value-and-the-weight-of-sampled-element/36992/2 "2020-04-03T23:00:15Z")

</div>

```julia
sample(collect(zip(values, weights)), Weights(weights))

```

should give you want you want.

---

<div class="post-metadata">

**Author:** ![sergevic](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sergevic/32/9958_2.png) [@sergevic](https://discourse.julialang.org/u/sergevic)\
**Post date:** [April 3, 2020, 11:50pm UTC](https://discourse.julialang.org/t/how-to-return-the-value-and-the-weight-of-sampled-element/36992/3 "2020-04-03T23:50:13Z")

</div>

> [@anon94023334](#):
>
> sample(collect(zip(values, weights)), Weights(weights))

Wonderful! That’s exactly what I need! Thanks!

---

<div class="post-metadata">

**Author:** ![Skoffer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/skoffer/32/378_2.png) [@Skoffer](https://discourse.julialang.org/u/Skoffer)\
**Post date:** [April 4, 2020, 7:40am UTC](https://discourse.julialang.org/t/how-to-return-the-value-and-the-weight-of-sampled-element/36992/4 "2020-04-04T07:40:23Z")

</div>

Less memory intensive version could be

```julia
idx = sample(Weights(weights))
(values[idx], weights[idx])

```

---

<div class="post-metadata">

**Author:** ![anon94023334](https://avatars.discourse-cdn.com/v4/letter/a/e274bd/32.png) [@anon94023334](https://discourse.julialang.org/u/anon94023334)\
**Post date:** [April 4, 2020, 6:56pm UTC](https://discourse.julialang.org/t/how-to-return-the-value-and-the-weight-of-sampled-element/36992/5 "2020-04-04T18:56:55Z")

</div>

This should be marked as the correct answer. Nicely done.

---

<div class="post-metadata">

**Author:** ![sergevic](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sergevic/32/9958_2.png) [@sergevic](https://discourse.julialang.org/u/sergevic)\
**Post date:** [April 5, 2020, 5:37am UTC](https://discourse.julialang.org/t/how-to-return-the-value-and-the-weight-of-sampled-element/36992/6 "2020-04-05T05:37:49Z")

</div>

Oh wow! I like this solution! Specially the less memory intensive part 🙂 I like both approaches anyway!
