Ritu Lahkar
Ritu Lahkar

Reputation: 463

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

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)

Upvotes: 3

Views: 335

Answers (2)

Saxasmu
Saxasmu

Reputation: 384

Very short version:

julia> a=(1,2)

julia> b=(3,4)

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

(1, 2, 3, 4)

Upvotes: 2

Bogumił Kamiński
Bogumił Kamiński

Reputation: 69879

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.

Upvotes: 3

Related Questions