-2

If I have code which produces a list of lists for example something like

import numpy as np
list=np.linspace(0,100,num=100)
n=100
for j in range(2,n):
        samples=[]
        for i in range(1,j): 
            samples.append(list[i])  
        print(samples) 

Which produces output like the following:

[0,1]
[0,1,2]
[0,1,2,3]
.
.
.
[0,1,2,...,100]

How do I amend my code so that it only returns the last list in the output which would be the list [0,1,2,...,100]?

LOC12345
  • 47
  • 1
  • 1
  • 4

1 Answers1

1

Using negative indices like -1 (last element):

import numpy as np
list=np.linspace(0,100,num=100)
n=100
for j in range(2,n):
        samples=[]
        for i in range(1,j): 
            samples.append(list[i])  
        print(samples[-1])
vgar
  • 171
  • 6