# Dictionary from a String Sequence Iteration

**URL:** https://discourse.julialang.org/t/dictionary-from-a-string-sequence-iteration/74359
**Category:** General Usage
**Tags:** strings, dictionary
**Created:** [January 10, 2022, 8:16pm UTC](https://discourse.julialang.org/t/dictionary-from-a-string-sequence-iteration/74359 "2022-01-10T20:16:40Z")
**Posts on this page:** 8
**Page:** 1

<div class="post-metadata">

### Author: ![Jdbeck66](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jdbeck66/32/19399_2.png) [@Jdbeck66](https://discourse.julialang.org/u/Jdbeck66)
#### Post date: [January 10, 2022, 8:16pm UTC](https://discourse.julialang.org/t/dictionary-from-a-string-sequence-iteration/74359/1 "2022-01-10T20:16:40Z")

</div>

I’m looking for a more elegant, maybe faster way to create dictionary of position =\> difference between two string sequences. This is what I’m currently using.

```julia
 function mutationpositions(wildtype, variant)
    dict = Dict{Int,Char}();
    for i ∈ 1:length(wildtype)
        if wildtype[i] != variant[i]
                push!(dict, i => variant[i])
        end
    end
    return dict
end

```

I thought maybe I could use a list comprehension with `zip` kind of like this:

`count(((a,b),) -> a != b, zip(sequence₁,sequence₂))`

but can’t seem to figure out how to capture the position.

Thanks for any help.

JB

---

<div class="post-metadata">

### Author: ![oheil](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oheil/32/220745_2.png) [@oheil](https://discourse.julialang.org/u/oheil)
#### Post date: [January 10, 2022, 8:53pm UTC](https://discourse.julialang.org/t/dictionary-from-a-string-sequence-iteration/74359/2 "2022-01-10T20:53:01Z")

</div>

~~I don't know if this is faster and it seems that `wildtype` and `variant` have to be of same length. So perhaps you like this, but it doesn't create a Dict:

```julia
bv = split(wildtype,"") .== split(variant,"")
indices = findall( x->x==0, bv)
variants = split(variant,"")[.!bv]

```

 ~~ ;-) Stefan is right:

---

<div class="post-metadata">

### Author: ![StefanKarpinski](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stefankarpinski/32/24_2.png) [@StefanKarpinski](https://discourse.julialang.org/u/StefanKarpinski)
#### Post date: [January 10, 2022, 9:00pm UTC](https://discourse.julialang.org/t/dictionary-from-a-string-sequence-iteration/74359/3 "2022-01-10T21:00:23Z")

</div>

It’s generally not recommended to do genome stuff with strings. It’s an inefficient representation since you only need two bits per DNA nucleotide and strings have to handle the complexity of potentially holding Unicode data, which cannot happen with DNA. Consider using the BioJulia packages designed for genetic data:

[https://biojulia.github.io/Bio.jl/man/seq/](https://biojulia.github.io/Bio.jl/man/seq/)

---

<div class="post-metadata">

### Author: ![tfiers](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tfiers/32/32427_2.png) [@tfiers](https://discourse.julialang.org/u/tfiers)
#### Post date: [January 10, 2022, 9:17pm UTC](https://discourse.julialang.org/t/dictionary-from-a-string-sequence-iteration/74359/4 "2022-01-10T21:17:45Z")

</div>

[for me, that link should be [Biological Sequences - Bio.jl](https://biojulia.net/Bio.jl/stable/man/seq/) – or rather [Biological Symbols · BioSequences.jl](https://biojulia.net/BioSequences.jl/stable/symbols/)]

---

<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: [January 10, 2022, 9:32pm UTC](https://discourse.julialang.org/t/dictionary-from-a-string-sequence-iteration/74359/5 "2022-01-10T21:32:01Z")

</div>

without prejudice to all the advice to use the suitable package, a function (toy) to do what you ask could be the following:

```julia
findeachpos(s1,s2)=(first(p) for p in enumerate(zip(s1,s2)) if last(p)[1]!=last(p)[2])
collect(findeachpos(s1,s2))

```

---

<div class="post-metadata">

### Author: ![tfiers](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tfiers/32/32427_2.png) [@tfiers](https://discourse.julialang.org/u/tfiers)
#### Post date: [January 10, 2022, 10:49pm UTC](https://discourse.julialang.org/t/dictionary-from-a-string-sequence-iteration/74359/6 "2022-01-10T22:49:25Z")

</div>

Or with unpacking/destructuring to variable names:

```julia
mutationpositions = Dict(
    i => v for (i, (w, v)) in
    enumerate(zip(wildtype, variant))
    if w != v
)

```

---

<div class="post-metadata">

### Author: ![StefanKarpinski](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stefankarpinski/32/24_2.png) [@StefanKarpinski](https://discourse.julialang.org/u/StefanKarpinski)
#### Post date: [January 11, 2022, 1:49am UTC](https://discourse.julialang.org/t/dictionary-from-a-string-sequence-iteration/74359/7 "2022-01-11T01:49:57Z")

</div>

Weirdly I copied a URL from the browser so not sure why it would later not work in the same browser.

---

<div class="post-metadata">

### Author: ![Jdbeck66](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jdbeck66/32/19399_2.png) [@Jdbeck66](https://discourse.julialang.org/u/Jdbeck66)
#### Post date: [January 11, 2022, 3:56pm UTC](https://discourse.julialang.org/t/dictionary-from-a-string-sequence-iteration/74359/8 "2022-01-11T15:56:44Z")

</div>

Thanks for the info. I’ve been planning on checking out the biojulia package. I have a code base already written that I’ll have to refactor. And it was as much about learning code alternatives. I did not even think to wrap `enumerate` around `zip` - that’s the idea I was looking for.

Thanks again - this discourse community is the best I’ve worked with. Very helpful.
