combining array and list elemint by index

Question:

I have an array (which comes from a kdtree):

array =  [[a b c d e]
          [a b c d e]
          [a b c d e]]

and a list :

lst = [1, 2, 3, 4, 5]

I want to do some list comprehension (using array and lst) that makes it look like this:

desired_result = [[a, b, c, d, e, 1]
                  [a, b, c, d, e, 2]
                  [a, b, c, d, e, 3]]

I am familiar with list comprehension just not familiar enough to know how to deal with this.

Asked By: JaredCusick

||

Answers:

If you want a list comprehension:

result = [l+[x] for l,x in zip(array, lst)]
Answered By: mozway

An alternative way to list comprehension if array is of the type numpy.ndarray:

result = numpy.c_[array, lst]

Please check this answer for more details.

Answered By: medium-dimensional
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.