Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Splitting a python list with common end points

Suppose I have a python list as: points_list = [1, 2, 3, 4, 5, 6, 7, 8, 9,1]

And I need to split this list containing the number of elements in the index_list=[2, 2, 6, 3] But having common endpoints

That is:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

  • First 2 elements from points_list : [1,2]
  • Next 2 elements from the points_list but it should start from the place it stopped before : [2,3]
  • Then the next 6 elements : [3,4,5,6,7,8]
  • Finally, 3 elements in the same way : [8,9,1]

Ultimately, what I expect is to have something like: [[1,2],[2,3],[3,4,5,6,7,8],[8,9,1]] which corresponds to the number of elements mentioned in the index_list=[2, 2, 6, 3]

Can you please help me to perform this task

>Solution :

Here’s my code:

points_list = [1, 2, 3, 4, 5, 6, 7, 8, 9,1]
index_list=[2, 2, 6, 3]
res = []
end_point = 0
for i in index_list:
    temp_list = points_list[end_point:end_point+i] # Get the list
    res.append(temp_list) # Append it
    end_point = points_list.index(temp_list[-1]) # Get the index of the last value
print(res)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading