# Best way to generate vector of tuples based on two vectors

**URL:** <https://discourse.julialang.org/t/best-way-to-generate-vector-of-tuples-based-on-two-vectors/117687>\
**Category:** Performance\
**Tags:** error, cplex, try-catch, using, highs\
**Created:** [July 31, 2024, 5:11pm UTC](https://discourse.julialang.org/t/best-way-to-generate-vector-of-tuples-based-on-two-vectors/117687 "2024-07-31T17:11:09Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![K5Julien](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/k5julien/32/210626_2.png) [@K5Julien](https://discourse.julialang.org/u/K5Julien)\
**Post date:** [July 31, 2024, 5:11pm UTC](https://discourse.julialang.org/t/best-way-to-generate-vector-of-tuples-based-on-two-vectors/117687/1 "2024-07-31T17:11:09Z")

</div>

The following does what I want, generate a vector of tuples thanks to the two vectors that I provide.

`xycoordinates = Tuple{Int,Int}[(x,y) for x in collect(1:10), y in rand(1:10, 10)]`

I wanted to know if there exists a better way for doing this or is it already optimal, in terms of code readability and conciseness.

---

<div class="post-metadata">

**Author:** ![DanielVandH](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/danielvandh/32/31134_2.png) [@DanielVandH](https://discourse.julialang.org/u/DanielVandH)\
**Post date:** [July 31, 2024, 5:13pm UTC](https://discourse.julialang.org/t/best-way-to-generate-vector-of-tuples-based-on-two-vectors/117687/2 "2024-07-31T17:13:48Z")

</div>

`tuple.(x, y)`? Though I think you are creating `tuple.(x, y')` in your example actually

---

<div class="post-metadata">

**Author:** ![K5Julien](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/k5julien/32/210626_2.png) [@K5Julien](https://discourse.julialang.org/u/K5Julien)\
**Post date:** [July 31, 2024, 5:15pm UTC](https://discourse.julialang.org/t/best-way-to-generate-vector-of-tuples-based-on-two-vectors/117687/3 "2024-07-31T17:15:17Z")

</div>

Thanks! Plus there was a mistake in my code, I generated a matrix instead of a vector.

---

<div class="post-metadata">

**Author:** ![matthias314](https://avatars.discourse-cdn.com/v4/letter/m/a88e4f/32.png) [@matthias314](https://discourse.julialang.org/u/matthias314)\
**Post date:** [July 31, 2024, 5:37pm UTC](https://discourse.julialang.org/t/best-way-to-generate-vector-of-tuples-based-on-two-vectors/117687/4 "2024-07-31T17:37:19Z")

</div>

A side remark: There are some unnecessary allocations in your code. You can just use `1:10` instead of `collect(1:10)`. And `rand(1:10, 10)` also creates a vector that it not really needed.

I’m not sure if you want a vector with 10 or with 100 elements as the result. In the first case you can say

```julia
[(x, rand(1:10)) for x in 1:10]

```

and in the second

```julia
[(x, rand(1:10)) for x in 1:10 for _ in 1:10]

```

---

<div class="post-metadata">

**Author:** ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)\
**Post date:** [July 31, 2024, 5:58pm UTC](https://discourse.julialang.org/t/best-way-to-generate-vector-of-tuples-based-on-two-vectors/117687/5 "2024-07-31T17:58:21Z")

</div>

Indeed, and also, the type annotation is redundant here (as also demonstrated by your example)

> [@K5Julien](#):
>
> ```julia
> Tuple{Int,Int}[(x,y) for x in collect(1:10), y in rand(1:10, 10)]
> 
> ```

One can simply allow the comprehension to determine the type automatically.
