# How to check symmetry inside tuples and avoide overwrtting?

**URL:** <https://discourse.julialang.org/t/how-to-check-symmetry-inside-tuples-and-avoide-overwrtting/98694>\
**Category:** General Usage\
**Tags:** question, tuple, dictionary\
**Created:** [May 11, 2023, 5:17pm UTC](https://discourse.julialang.org/t/how-to-check-symmetry-inside-tuples-and-avoide-overwrtting/98694 "2023-05-11T17:17:31Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Optimization](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/optimization/32/32462_2.png) [@Optimization](https://discourse.julialang.org/u/Optimization)\
**Post date:** [May 11, 2023, 5:17pm UTC](https://discourse.julialang.org/t/how-to-check-symmetry-inside-tuples-and-avoide-overwrtting/98694/1 "2023-05-11T17:17:31Z")

</div>

Is there any easy way to check if you have symmetric tuples in your lists?

Like auto checking that `[(2,3),(3,5),(3,2),..., [10,8)]` has. Because of `(2,3) ` and `(3,2)`. Also I don’r have any tuples with same number as first and second elements

---

<div class="post-metadata">

**Author:** ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)\
**Post date:** [May 11, 2023, 5:32pm UTC](https://discourse.julialang.org/t/how-to-check-symmetry-inside-tuples-and-avoide-overwrtting/98694/2 "2023-05-11T17:32:25Z")

</div>

“Easy” in what sense? You can write a simple function that does exactly that, for example:

```julia
julia> function bad_tups(tups)
           for i in firstindex(tups):lastindex(tups)
               tups[i][1] == tups[i][2] && return true
               for j in i+1:lastindex(tups)
                   if (tups[i][1] == tups[j][2] && tups[i][2] == tups[j][1])
                       return true
                   end
               end
           end
           return false
       end
bad_tups (generic function with 1 method)

julia> tups = [(2,3),(3,5),(3,2)]
3-element Vector{Tuple{Int64, Int64}}:
 (2, 3)
 (3, 5)
 (3, 2)

julia> bad_tups(tups)
true

```

---

<div class="post-metadata">

**Author:** ![Optimization](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/optimization/32/32462_2.png) [@Optimization](https://discourse.julialang.org/u/Optimization)\
**Post date:** [May 11, 2023, 5:59pm UTC](https://discourse.julialang.org/t/how-to-check-symmetry-inside-tuples-and-avoide-overwrtting/98694/3 "2023-05-11T17:59:11Z")

</div>

Thank @lmiq indeed I have a dictionary that maps some ids to some list of tuples. And having symmetic tuples in the values cause some problem. I’m looking for a way to loop trough all the values in the dictionary and then check the values, if they had symmetric. I want to reduce the values to all symetric tuples. For example:

```julia
# Letter is a vectors of string 
# numbers is a dictionary mapping some numers to some vector of tuples of (int64,int64)

sym_tup = Dict()
for n in letters
    for (i,j) in numbers[n]
        if (j,i) in numbers[n]
            sym_tup = [(j,i)]
        end
    end
end

```

This overwrite the values of symmetric tuples. How to avide that?

---

<div class="post-metadata">

**Author:** ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)\
**Post date:** [May 11, 2023, 6:14pm UTC](https://discourse.julialang.org/t/how-to-check-symmetry-inside-tuples-and-avoide-overwrtting/98694/4 "2023-05-11T18:14:16Z")

</div>

It is better if you provide an example of the type of collection you want, and the result expected. For instance, the first proposal there does not work for your case because the dictionary is not indexable, and that was misleading in your original post. So you will get better advice by being very specific in what you have and what you want as a result.

---

<div class="post-metadata">

**Author:** ![GunnarFarneback](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gunnarfarneback/32/1827_2.png) [@GunnarFarneback](https://discourse.julialang.org/u/GunnarFarneback)\
**Post date:** [May 11, 2023, 6:19pm UTC](https://discourse.julialang.org/t/how-to-check-symmetry-inside-tuples-and-avoide-overwrtting/98694/5 "2023-05-11T18:19:39Z")

</div>

```julia
julia> x = [(2, 3), (3, 5), (3, 2), (10, 8)]
4-element Vector{Tuple{Int64, Int64}}:
 (2, 3)
 (3, 5)
 (3, 2)
 (10, 8)

julia> intersect(x, reverse.(x))
2-element Vector{Tuple{Int64, Int64}}:
 (2, 3)
 (3, 2)

```

---

<div class="post-metadata">

**Author:** ![rocco\_sprmnt21](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rocco_sprmnt21/32/20127_2.png) [@rocco\_sprmnt21](https://discourse.julialang.org/u/rocco_sprmnt21)\
**Post date:** [May 11, 2023, 9:18pm UTC](https://discourse.julialang.org/t/how-to-check-symmetry-inside-tuples-and-avoide-overwrtting/98694/6 "2023-05-11T21:18:36Z")

</div>

try this way

```julia
for n in letter
    for (i,j) in numbers[n]
        if (j,i) in numbers[n]
            push!(get!(()->[],sym_tup,n), (i,j))
        end
    end
end

```

It’s not meant to be a “solution” to the problem, just an answer to how to make this piece of code work, at least as far as I understand from the comments ###

```julia
# Letter is a vectors of string 
# numbers is a dictionary mapping some numers to some vector of tuples of (int64,int64)

```

---

<div class="post-metadata">

**Author:** ![rocco\_sprmnt21](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rocco_sprmnt21/32/20127_2.png) [@rocco\_sprmnt21](https://discourse.julialang.org/u/rocco_sprmnt21)\
**Post date:** [May 12, 2023, 6:52am UTC](https://discourse.julialang.org/t/how-to-check-symmetry-inside-tuples-and-avoide-overwrtting/98694/7 "2023-05-12T06:52:50Z")

</div>

i tried the new 1.9.0 version of julia with this script, but i get the following strange screens.  
And the for loop doesn’t finish until I “kill” it with ctrl-z

```julia
letter=string.(Set(rand(10:20, 5)))
numbers=Dict{String, Vector{Tuple{Int64,Int64}}}()

t=filter(e->-(e...)!=0,[(rand(1:5),rand(1:5)) for _ in 1:100])

[numbers[l]=unique(t[rand(1:length(t),10)]) for l in letter]

sym_tup = Dict()

for n in letter
    for (i,j) in numbers[n]
        if (j,i) in numbers[n]
            push!(get!(()->[],sym_tup,n), (i,j))
        end
    end
end

```

```julia

#87[numbers[l]=unique(t[rand(1:length(t),10)]) for l in letter]    
4-element Vector{Vector{Tuple{Int64, Int64}}}:
 [(3, 5), (2, 5), (2, 3), (5, 4), (1, 5), (1, 2), (3, 1), (2, 4), (3, 4), (4, 1)]
 [(4, 5), (4, 2), (3, 4), (3, 2), (1, 5), (2, 3), (1, 3), (2, 5)]   
 [(1, 5), (5, 4), (4, 2), (3, 4), (3, 5), (4, 5), (5, 2), (5, 3), (2, 1)]
 [(1, 4), (4, 5), (4, 1), (2, 5), (4, 2), (2, 3), (1, 2)]

#87sym_tup = Dict()
Dict{Any, Any}()

#87for n in letter

```

this is my system

```julia
julia> versioninfo()
Julia Version 1.9.0
Commit 8e63055292 (2023-05-07 11:25 UTC)
Platform Info:
  OS: Windows (x86_64-w64-mingw32)
  CPU: 8 × 11th Gen Intel(R) Core(TM) i7-1165G7 @ 2.80GHz
  WORD_SIZE: 64
  LIBM: libopenlibm
  LLVM: libLLVM-14.0.6 (ORCJIT, tigerlake)
  Threads: 4 on 8 virtual cores
Environment:
  JULIA_EDITOR = code
  JULIA_NUM_THREADS = 4

```
