I'm trying to combine an mxn array called data with a list of m elements called cluster_data such that each element in the list cluster_data is appended as the last element of each of the rows in data.
As an example, I would want something like to combine
data = [[1,2,3,4],[5,6,7,8],[9,10,11,12],[13,14,15,16],[17,18,19,20]]
cluster_data = [1,2,3,4,5]
Such that
final_data = [[1,2,3,4,1],[5,6,7,8,2],[9,10,11,12,3],[13,14,15,16,4],[17,18,19,20,5]]
I have written some code that does this, but I was hoping for a more Pythonic way.
data_with_clusters = []
for i, row in enumerate(data):
row.append(cluster_data[i])
data_with_clusters.append(row)
My best guess so far, which doesn't work, is:
data_with_clusters = [row.append(cluster_data[i]) for i, row in enumerate(data)]