How to append a list with arrays of two different sizes based on conditons
I was wondering how to do this in a more efficient way for arbitrary arrays, the code is written in PyTorch, but it is only for 1-d tensors. 
Thank you!

test=[]
data=np.random.uniform(0,1,[20,])
x=torch.from_numpy(data).float()
x,_=torch.sort(x)
v=torch.rand(5).float()
v,_=torch.sort(v)
for i in range(len(x)):
    if x[i] &lt; v[0]:
         test.append(v[0])
    elif x[i] &lt; v[1]:
         test.append(v[1])
    elif x[i] &lt; v[2]:
           test.append(v[2])
    elif x[i] &lt; v[3]:
           test.append(v[3])
    else: 
        test.append(v[4])



  test


you can use the built-in function next: 

for i in x:
    test.append(next((e for e in v[:4] if  i &lt; e), v[4]))


you can also use a list comprehension instead of for loop:

s = v[:4]
d = v[4]
test = [next((e for e in s if  i &lt; e), d)) for i in x]


if the test variable has already some elements you can use the in-place assignment += operator:

test += [next((e for e in s if  i &lt; e), d) for i in x]

