Saturday, 10 August 2013

Python lists get specific length of elements from index

Python lists get specific length of elements from index

my_list = [1,2,3,4,5,6,7,8,9,10,11,12,13]
I need to obtain a specific length of elements form a list starting at a
specific index in Python. For instance I would like to get the three next
elements from the [2] element above. Is there anyway to get the three
elements from the specific index? I wont always know the next amount of
elements I want to get, sometimes I may want to get two elements,
sometimes eight elements, so x elements.
I know I can do my_list[2:] to get all of the elements from the third
element to the end of the list. What I want to do is specify how many
elements to read after the third element. Conceptually in my mind the
example above would look like my_list[2:+3] however I know this wont work.
How can I achieve this or is it better to define my own function to give
me this functionality?

No comments:

Post a Comment