# Iterate over collection of tuples

**URL:** <https://discourse.julialang.org/t/iterate-over-collection-of-tuples/7321>\
**Category:** General Usage\
**Created:** [November 26, 2017, 12:22pm UTC](https://discourse.julialang.org/t/iterate-over-collection-of-tuples/7321 "2017-11-26T12:22:00Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Di11on](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/di11on/32/6352_2.png) [@Di11on](https://discourse.julialang.org/u/Di11on)\
**Post date:** [November 26, 2017, 12:22pm UTC](https://discourse.julialang.org/t/iterate-over-collection-of-tuples/7321/1 "2017-11-26T12:22:00Z")

</div>

Hi folks,

I am new to Julia and we are developing an open source framework for developing and using energy system optimization models. We are using JuMP. We have all been working mostly with GAMS untill now.

We are using a generic entity-relationship data model and I would like to develop the most efficient/compact way of iterating over a set of tuples (relationships).

For example, I have a set of entities of type e1 and another set of entities of type e2. I define a relationship that relates entities of type e1 to entites of type e2 which will essentially be a collection of pairs of entites of types e1 and e2.

Now I want to iterate over these pairs of entities… so something like

for o1,o2 in rel\_e1\_to\_e2

end

so what does rel\_e1\_to\_e2 look like and what is the proper syntax of the for loop statement?

Thanks in advance!

---

<div class="post-metadata">

**Author:** ![kristoffer.carlsson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kristoffer.carlsson/32/22_2.png) [@kristoffer.carlsson](https://discourse.julialang.org/u/kristoffer.carlsson)\
**Post date:** [November 26, 2017, 12:25pm UTC](https://discourse.julialang.org/t/iterate-over-collection-of-tuples/7321/2 "2017-11-26T12:25:11Z")

</div>

Do you mean `zip`?

---

<div class="post-metadata">

**Author:** ![Di11on](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/di11on/32/6352_2.png) [@Di11on](https://discourse.julialang.org/u/Di11on)\
**Post date:** [November 26, 2017, 3:54pm UTC](https://discourse.julialang.org/t/iterate-over-collection-of-tuples/7321/3 "2017-11-26T15:54:41Z")

</div>

As I understand it, zip will take two collections and combine them… i.e. turn 1,2,3,4 and a,b,c,d into:  
(1,a)  
(2,b)  
(3,c)  
(4,d)

Instead, I want to create something that contains only specific pairs and then iterate over them. E.g.

(2,e)  
(4,a)

---

<div class="post-metadata">

**Author:** ![kristoffer.carlsson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kristoffer.carlsson/32/22_2.png) [@kristoffer.carlsson](https://discourse.julialang.org/u/kristoffer.carlsson)\
**Post date:** [November 26, 2017, 3:58pm UTC](https://discourse.julialang.org/t/iterate-over-collection-of-tuples/7321/4 "2017-11-26T15:58:41Z")

</div>

Could you give a concrete example where the data structures are defined.

A vector of tuples seems to fit in with what you want but that seems to “easy”?

```julia
julia> V = [("a",3), ("b",5)]
2-element Array{Tuple{String,Int64},1}:
 ("a", 3)
 ("b", 5)

julia> for (x, i) in V
           @show x, i
       end
(x, i) = ("a", 3)
(x, i) = ("b", 5)

```
