How to interate over two or more vectors or tuples in julia?

Viewed 78

Can we iterate over two or more vectors or tuples in julia?

julia> c=Tuple(x for x in a, b)

The above code does not work but shows what i want to do. I need to iterate over both a and b one after other.

Suppose,

julia> a=(1,2)

julia> b=(3,4)

and I want c to be:

julia> c=(1,2,3,4)
2 Answers

Use:

julia> c = Tuple(Iterators.flatten((a, b)))
(1, 2, 3, 4)

to get a Tuple as you requested. But if you are OK with a lazy iterator then just Iterators.flatten((a, b)) is enough.

Very short version:

julia> a=(1,2)

julia> b=(3,4)

julia> c = (a..., b...)

(1, 2, 3, 4)

Related