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 :
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]