Justin Jones
Justin Jones

Reputation: 361

Removing elements from list

I have an array of length 900 and I want to remove the last 200 elements. I do not mind creating a new array but I want to code to be an concise and efficient as possible.

    f = [1,2,3,4,5,3,2,3,2,4,5,2....] #random one digit numbers of length 400. 
    t=400
    x=200
    while(t>x):
        f = np.delete(f,t)
        t = t-1

while this certainly works, I am looking for something that will preform the same task in less lines or at greater speed.

Upvotes: 0

Views: 58

Answers (2)

pat
pat

Reputation: 12749

You can also delete the elements without creating a new list:

del f[-200:]

Upvotes: 3

Ajax1234
Ajax1234

Reputation: 71451

You can use list slicing:

f = [1,2,3,4,5,3,2,3,2,4,5,2....]

f = f[:-200]

Upvotes: 3

Related Questions