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

Return all values from range() function and add them into an array

I’m trying to make a program which extracts specific PDF pages from a PDF file. To do this, I have an array where I put all page numbers I want to extract. However, to spare myself some time, I want to be able to add all pages between two page numbers (between 1 and 8 for instance).

Currently I have the following code:

def r(n1, n2):
    return range(n1, n2)

if __name__ == '__main__':
    pages_to_keep = [1, 2, 10, r(12,14)]
    print pages_to_keep

The output 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

[1, 2, 10, [12, 13]]

but what I want is:

[1, 2, 10, 12, 13, 14]

The thought here is that the r function can be called in the array to easily add multiple pages, but I want to get rid of the brackets which gets returned from the function. Is there a way to do this?

>Solution :

There are two methods of doing so:

  1. pages_to_keep = [1, 2, 10]+list(r(12,14))
pages_to_keep = [1, 2, 10]
pages_to_keep.extend(list(r(12,14)))
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