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

Basic list iteration in python

Very new to python. I am trying to iterate over a list of floating points and append elements to a new list based on a condition. Everytime I populate the list I get double the output, for example a list with three floating points gives me an output of six elements in the new list.

tempretures = [39.3, 38.2, 38.1]

new_list = []
i=0
while i <len(tempretures):
    for tempreture in range(len(tempretures)):
        if tempreture < 38.3:
            new_list = new_list + ['L']
        elif tempreture > 39.2:
            new_list = new_list + ['H']
        else: tempreture (38.3>=39.2)
        new_list = new_list + ['N']
        i=i+1

print (new_list)
print (i)
['L', 'N', 'L', 'N', 'L', 'N']
3

>Solution :

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

It looks like you’re looking for something like the below list comprehension, which should be a go to when you have a pattern of building a new list based on the values in an existing list.

new_list = ['L' if t < 38.3 else 'H' if t > 39.2 else 'N' for t in temperatures]
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