# How to iterate over Vector{String} elements and append a string to them

**URL:** https://discourse.julialang.org/t/how-to-iterate-over-vector-string-elements-and-append-a-string-to-them/125616
**Category:** New to Julia
**Created:** [February 6, 2025, 11:32am UTC](https://discourse.julialang.org/t/how-to-iterate-over-vector-string-elements-and-append-a-string-to-them/125616 "2025-02-06T11:32:35Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![mocalvao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mocalvao/32/19318_2.png) [@mocalvao](https://discourse.julialang.org/u/mocalvao)
#### Post date: [February 6, 2025, 11:32am UTC](https://discourse.julialang.org/t/how-to-iterate-over-vector-string-elements-and-append-a-string-to-them/125616/1 "2025-02-06T11:32:35Z")

</div>

Hi,  
I have the Vector{String}, e.g.,

```julia
vec = ["P1", "P2", ..., "Pn", "SC", "TC"]

```

and I would like therefrom to create the corresponding modified Vector{String}:

```julia
vec_mod = ["P1_O", "P1_D", "P2_O", "P2_D", ..., "Pn_O", "SC_O", "SC_D", "TC_O", "TC_D"]

```

How do I implement this generically?  
Thanks in advance.

---

<div class="post-metadata">

### Author: ![hendri54](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/hendri54/32/9621_2.png) [@hendri54](https://discourse.julialang.org/u/hendri54)
#### Post date: [February 6, 2025, 12:16pm UTC](https://discourse.julialang.org/t/how-to-iterate-over-vector-string-elements-and-append-a-string-to-them/125616/2 "2025-02-06T12:16:11Z")

</div>

Here I’m assuming you meant that the last entry in the “P” series was “Pn\_D” instead of “Pn\_O”. Then how about

```julia
julia> vec = ["P1", "P2", "P3", "P4", "SC", "TC"]
6-element Vector{String}:
 "P1"
 "P2"
 "P3"
 "P4"
 "SC"
 "TC"

julia> vec_mod = [x * suffix for x in vec for suffix in ["_O", "_D"]]
12-element Vector{String}:
 "P1_O"
 "P1_D"
 "P2_O"
 "P2_D"
 "P3_O"
 "P3_D"
 "P4_O"
 "P4_D"
 "SC_O"
 "SC_D"
 "TC_O"
 "TC_D"

```

---

<div class="post-metadata">

### Author: ![mocalvao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mocalvao/32/19318_2.png) [@mocalvao](https://discourse.julialang.org/u/mocalvao)
#### Post date: [February 6, 2025, 12:24pm UTC](https://discourse.julialang.org/t/how-to-iterate-over-vector-string-elements-and-append-a-string-to-them/125616/3 "2025-02-06T12:24:45Z")

</div>

That’s what I really meant in my OP. So simple (the power of comprehensions)! Thanks, @hendri54

---

<div class="post-metadata">

### Author: ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)
#### Post date: [February 6, 2025, 1:03pm UTC](https://discourse.julialang.org/t/how-to-iterate-over-vector-string-elements-and-append-a-string-to-them/125616/5 "2025-02-06T13:03:41Z")

</div>

FWIW, multiplication could be broadcasted to achieve the same result:

```julia
v = ["P1", "P2", "Pn", "SC", "TC"]
s = ["_O" "_D"]
vec(permutedims(v .* s))

```

---

<div class="post-metadata">

### Author: ![mocalvao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mocalvao/32/19318_2.png) [@mocalvao](https://discourse.julialang.org/u/mocalvao)
#### Post date: [February 6, 2025, 1:56pm UTC](https://discourse.julialang.org/t/how-to-iterate-over-vector-string-elements-and-append-a-string-to-them/125616/6 "2025-02-06T13:56:03Z")

</div>

@rafael.guerra:Thanks for the further enlightenment. However, I have to confess I find @hendri54’s accepted solution more understandable…
