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

Divide list in 2?

As a result of my analysis, I will have a list where every measurement will occur twice. There might be double measurements (1 is measured twice in this case)

example_list = [1, 2, 3, 4, 1, 1, 2, 1, 3, 4]

My desired outcome:

outcome = [1, 2, 3, 4, 1]

Order does not matter, as long as every measurement only occurs once.

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

Since there might be double measurements, I cannot use unique().

Is there a way to efficiently half the list so every measurement only occurs once?

Edit for clarification: The list elements from double measurements will occur 4 (in this case) or more times, the ones from repeated measurements will occur twice

>Solution :

If there is no logic as to where the elements are in the list that need to be discarded, then one option is to use Counter, and halve the counts:

from collections import Counter

c = Counter(example_list)
for key in c:
    c[key] //= 2
lst = list(c.elements())

For your example input, lst will be [1, 1, 2, 3, 4]

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