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

Take numbers from a list in a specific order in Python

I would like to remove the numbers from the list of three in three positions and then store them in a new list. I thought of doing something like n+3 but don´t know how to implement it.

[1, 1, 2, 2, 2, 3, 3, 3, 4, 4, 4, 5, 5, 5, 1, 6, 1, 4, 7, 2, 4, 8, 4, 6, 9, 6, 5]

This is my list and I would like to create a new list like this:

[1,2,3,4,5,6,7,8,9]

Thank you in advance

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 :

As far as I understand, you want to pick one element in every three elements. For that, you can use slicing [::3], where 3 means the step size:

lst = [1, 1, 2, 2, 2, 3, 3, 3, 4, 4, 4, 5, 5, 5, 1, 6, 1, 4, 7, 2, 4, 8, 4, 6, 9, 6, 5]

output = lst[::3]
print(output) # [1, 2, 3, 4, 5, 6, 7, 8, 9]
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