# How to concatenate 2 vectors in a third ...

Hi. I can't find in the documentation an elegant solution for this quite simple question :

a = vector([1,2,3])
b = vector([4,5])


How to find c = (1,2,3,4,5)

a+ b doesn't works like in Python ...

edit retag close merge delete

Sort by » oldest newest most voted sage: a = vector([1,2,3])
sage: b = vector([4,5])
sage: from itertools import chain
sage: c = vector(chain(a,b)); c
(1, 2, 3, 4, 5)

more

Smart ! Danke I will create & use a simple function :

concatene_vectors(a,b):
return vector(chain(a,b))


Click the "check mark" button near the answer to accept it and mark your question as solved.

Alternate solution :

sage: vector(vector([1, 2, 3]).list()+vector([4, 5]).list())
(1, 2, 3, 4, 5)


Hence :

sage: def concatenate_vectors(*x):
....:     return vector(reduce(lambda a,b:a+b, map(lambda u:u.list(), x)))
....:
sage: concatenate_vectors(vector([1, 2, 3]), vector([4, 5]), vector())
(1, 2, 3, 4, 5, 6)


HTH,

more

1

Or vector(list(a) + list(b)), using the notation in the original question.