user2171215
user2171215

Reputation:

How can I create a list from a part of an existing list on Python?

I have a list with 6899 numbers, from wich I have to create a new list containing only te terms from i=3200 to i=4121. Kind of like a copy and paste.

I tried the following:

e = a[3200<i<4121]

Being "e" the new list and "a" the original one, but this code returns a float, instead of a new list, any hints?

Upvotes: 0

Views: 77

Answers (1)

mgilson
mgilson

Reputation: 309841

You can use slice notation:

e = a[3200:4121]

There's also an optional step parameter:

e = a[3200:4121:1]

which is completely equivalent to the above, but you could use that form to get (for example) every other element starting at index 3200 and ending at index 4121 (not inclusive):

e = a[3200:4121:2]

Note that the "start" parameter is inclusive, but the "stop" parameter is exclusive. So, lst[n:m] will give you a list staring at index slice out indices n to m-1.

Upvotes: 5

Related Questions