# String splatting inside comprehensions

**URL:** https://discourse.julialang.org/t/string-splatting-inside-comprehensions/111464
**Category:** General Usage
**Tags:** strings, comprehension, splat
**Created:** [March 11, 2024, 2:59pm UTC](https://discourse.julialang.org/t/string-splatting-inside-comprehensions/111464 "2024-03-11T14:59:39Z")
**Posts on this page:** 12
**Page:** 1

<div class="post-metadata">

### Author: ![cnaak](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cnaak/32/10287_2.png) [@cnaak](https://discourse.julialang.org/u/cnaak)
#### Post date: [March 11, 2024, 2:59pm UTC](https://discourse.julialang.org/t/string-splatting-inside-comprehensions/111464/1 "2024-03-11T14:59:40Z")

</div>

The following excerpt (taken from a `julia-1.10.2` REPL) illustrates my question:

```julia
julia> src = ("Ab","Cd")
("Ab", "Cd")

julia> ["Ab"..., "Cd"...]
4-element Vector{Char}:
 'A': ASCII/Unicode U+0041 (category Lu: Letter, uppercase)
 'b': ASCII/Unicode U+0062 (category Ll: Letter, lowercase)
 'C': ASCII/Unicode U+0043 (category Lu: Letter, uppercase)
 'd': ASCII/Unicode U+0064 (category Ll: Letter, lowercase)

julia> [i... for i in src]
ERROR: syntax: "..." expression outside call around REPL[114]:1
Stacktrace:
 [1] top-level scope
   @ REPL[114]:1

```

Why can I manually splat `Strings` inside the `Vector` comprehension, but not so programatically? The error message indicates the lack of a call, but in the manual example, calls were lacking too.

How can I accomplish what was done manually, in a program?

---

<div class="post-metadata">

### Author: ![sijo](https://avatars.discourse-cdn.com/v4/letter/s/da6949/32.png) [@sijo](https://discourse.julialang.org/u/sijo)
#### Post date: [March 11, 2024, 3:11pm UTC](https://discourse.julialang.org/t/string-splatting-inside-comprehensions/111464/2 "2024-03-11T15:11:37Z")

</div>

When you write `["Ab"..., "Cd"...]` that’s syntax sugar for a call to the `vect` function. You can check this by executing `Meta.@lower ["Ab"..., "Cd"...]`. The two arguments are splatted in this call.

When you write `[2i for i in src]` the `2i` is not the argument of a function, it’s the body of the anonymous function to call for each `i`, so you can’t splat in that context.

For the case at hand you can do `vcat(collect.(src)...)`, or probably more efficient: `collect(Iterators.flatten(src))`.

---

<div class="post-metadata">

### Author: ![nilshg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nilshg/32/2283_2.png) [@nilshg](https://discourse.julialang.org/u/nilshg)
#### Post date: [March 11, 2024, 3:14pm UTC](https://discourse.julialang.org/t/string-splatting-inside-comprehensions/111464/3 "2024-03-11T15:14:06Z")

</div>

Or `collect(join(src))`

---

<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: [March 11, 2024, 3:37pm UTC](https://discourse.julialang.org/t/string-splatting-inside-comprehensions/111464/4 "2024-03-11T15:37:27Z")

</div>

This one is both compact and efficient:

```julia
vec(stack(src))   

```

---

<div class="post-metadata">

### Author: ![sijo](https://avatars.discourse-cdn.com/v4/letter/s/da6949/32.png) [@sijo](https://discourse.julialang.org/u/sijo)
#### Post date: [March 11, 2024, 3:40pm UTC](https://discourse.julialang.org/t/string-splatting-inside-comprehensions/111464/5 "2024-03-11T15:40:56Z")

</div>

It only works for strings of equal lengths though…

---

<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: [March 11, 2024, 3:45pm UTC](https://discourse.julialang.org/t/string-splatting-inside-comprehensions/111464/6 "2024-03-11T15:45:41Z")

</div>

Here’s another possibility. You have a lot of options.

```julia
reduce((spl,e)->vcat(spl,e...),src, init=Char[])

```

```julia
vcat([[e...] for e in src]...)

```

```julia
[[[e...] for e in src]...;]

```

```julia
vcat(Base.splat(vcat).(src)...)

```

---

<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: [March 11, 2024, 3:55pm UTC](https://discourse.julialang.org/t/string-splatting-inside-comprehensions/111464/7 "2024-03-11T15:55:50Z")

</div>

and also:

```julia
reduce(vcat, [i...] for i in src) 

```

or

```julia
[j for i in src for j in i] 	

```

---

<div class="post-metadata">

### Author: ![Alexander\_Knudson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/alexander_knudson/32/215656_2.png) [@Alexander\_Knudson](https://discourse.julialang.org/u/Alexander_Knudson)
#### Post date: [March 11, 2024, 4:59pm UTC](https://discourse.julialang.org/t/string-splatting-inside-comprehensions/111464/8 "2024-03-11T16:59:19Z")

</div>

Or even `collect(prod(src))` since string concatenation is defined via `*`

---

<div class="post-metadata">

### Author: ![cnaak](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cnaak/32/10287_2.png) [@cnaak](https://discourse.julialang.org/u/cnaak)
#### Post date: [March 11, 2024, 5:08pm UTC](https://discourse.julialang.org/t/string-splatting-inside-comprehensions/111464/9 "2024-03-11T17:08:32Z")

</div>

Great explanation! Thank you!!

---

<div class="post-metadata">

### Author: ![cnaak](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cnaak/32/10287_2.png) [@cnaak](https://discourse.julialang.org/u/cnaak)
#### Post date: [March 11, 2024, 5:10pm UTC](https://discourse.julialang.org/t/string-splatting-inside-comprehensions/111464/10 "2024-03-11T17:10:01Z")

</div>

Thank you for the concise and readable answer!!

---

<div class="post-metadata">

### Author: ![cnaak](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cnaak/32/10287_2.png) [@cnaak](https://discourse.julialang.org/u/cnaak)
#### Post date: [March 11, 2024, 5:19pm UTC](https://discourse.julialang.org/t/string-splatting-inside-comprehensions/111464/11 "2024-03-11T17:19:53Z")

</div>

Thank you for adding to the possibilities!

---

<div class="post-metadata">

### Author: ![cnaak](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cnaak/32/10287_2.png) [@cnaak](https://discourse.julialang.org/u/cnaak)
#### Post date: [March 11, 2024, 5:21pm UTC](https://discourse.julialang.org/t/string-splatting-inside-comprehensions/111464/12 "2024-03-11T17:21:52Z")

</div>

This is neat because it uses a comprehension, thus aligning with the original post. It also illustrates a flattening mechanism without using a function!
