Skip to content
Advertisement

Merge two Lists of different size

I have an issue with some part of my code, I’m fetching two apis and then mixing the results in one array:

Initialy the two arrays seems like this (although they can be empty or with more or less data):

print(foo)
    [[0, 0, 1, 2, 2, 2, 2, 2, 1, 2, 2, 2, 0, 2, 2, 2, 1, 2, 2, 2, 2, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 1, 2, 2, 0, 2, 0, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 1, 2, 0, 2, 0, 2, 2, 2, 0, 2, 2, 2, 2, 2, 2, 2, 2, 0, 2, 2, 2, 1, 0, 2, 2, 2, 0, 0, 2, 2, 2, 2, 2, 1, 0, 2, 2, 2, 1, 1], [0, 0, 1, 2, 2, 2, 2, 2, 1, 2, 2, 2, 0, 2, 2, 2, 1, 2, 2, 2, 2, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 1, 2, 2, 0, 2, 0, 2, 2, 2, 2, 2, 2, 2, 2 , 2, 2, 1, 2, 0, 2, 0, 2, 2, 2, 0, 2, 2, 2, 2, 2, 2, 2, 2, 0, 2, 2, 2, 1, 0, 2, 2, 2, 0, 0, 2, 2, 2, 2, 2, 1, 0, 2, 2, 2, 1, 1], [0, 0, 1, 2, 2, 2, 2, 2, 1, 2, 2, 2, 0, 2, 2, 2, 1, 2, 2, 
2, 2, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 1, 2, 2, 0, 2, 0, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 1, 2, 0, 2, 0, 2, 2, 2, 0, 2, 2, 2, 2, 2, 2, 2, 2, 0, 2, 2, 2, 1, 0, 2, 2, 2, 0, 0, 2, 2, 2, 2, 2, 1, 0, 2, 2, 2, 1, 1], [0, 0, 1, 2, 2, 2, 2, 2, 1, 2,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, 0, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 1, 2, 0, 2, 0, 2, 2, 2, 0, 2, 2, 2, 2, 2, 2, 2, 2, 0, 2, 2, 2, 1, 0, 2, 2, 2, 0, 0, 2, 2, 2, 2, 2, 1, 0, 2, 2, 2]]

print(bar)
    [[1, 1, 1, 2, 2, 1, 2, 1, 0, 1, 1, 1, 1, 1], [1, 1, 1, 0, 1, 1, 1, 1, 1, 0, 1, 1, 0, 1, 2, 2, 0, 1, 1, 0], [1, 1, 1, 1, 1, 1, 1, 2, 1, 1, 2, 1, 2, 1, 1, 1, 1, 0, 2, 2], [1, 0, 0, 0, 1, 2, 1, 2, 2, 2, 1, 1, 2, 0, 1, 1, 0, 0, 1, 1], [2, 0,
2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1], [2, 1, 1, 1, 2, 2, 2, 1, 2, 1, 2, 1, 1, 2, 1, 2, 1, 1, 2, 1], [1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 1, 2]] 

Their shapes are:

foo --> (7,) (the 2nd dimension varies on shape so numpy doesn't get it)
bar --> (7, 20) (In this case the sublists len() matches, but maybe won't in the future)
[len(i) for i in foo] --> [94, 95, 96, 96, 97, 96, 95] # not all sublists are equal in length
[len(i) for i in bar] --> [20, 20, 20, 20, 20, 20, 20]

So of course, when I try to append them in the 2nd dimension:

df = np.hstack([foo, bar]).tolist()

I get this error:

ValueError: all the input arrays must have same number of dimensions, but the array at index 0 has 2 dimension(s) and the array at index 1 has 1 dimension(s)

My question is what would be the best solution to this, trying to not loose data, maybe appending each sublist manually work but is it the only solution I have?

Advertisement

Answer

One array only has 1 dimension, the other has 2. But you can quickly adjust the first array to have dimensions (7,1) by using newaxis

For example:

df = np.hstack([foo[:, np.newaxis], bar]).tolist()
User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement