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

List comprehension in python with multiple if statements

I am trying to build list similar to this

Position = [1, 1, 1, 1, 1, 1, 1, 
      2, 2, 2, 2, 2, 2, 2, 
     3, 3, 3, 3, 3, 3, 3,
     4, 4, 4, 4, 4, 4, 4,
     5, 5, 5, 5, 5, 5, 5,
     6, 6, 6, 6, 6, 6, 6,
     7, 7, 7, 7, 7, 7, 7,
     8, 8, 8, 8, 8, 8, 8,
     9, 9, 9, 9, 9, 9, 9,
     10, 10, 10, 10, 10, 10, 10,
     11, 11, 11, 11, 11, 11, 11,
     12, 12, 12, 12, 12, 12, 12,
     13, 13, 13, 13, 13, 13, 13,
     14, 14, 14, 14, 14, 14, 14,
     15, 15, 15, 15, 15, 15, 15]

With the list comprehension like below (with one if statement), only the first 7 elements can be created. How can I add multiple if statements?

d = 1 
position = [d if i<7 else d+1 for i in range (105)]

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

>Solution :

No need for even a single if; just use integer division

[1+i//7 for i in range (7*15)]
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