1

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
2
  • why are you doing while i <len(tempretures):? Commented Jan 10, 2022 at 4:42
  • because the for loop gave me double the original inputs, i thought maybe this would stop the loop. obviously not. Commented Jan 10, 2022 at 4:46

4 Answers 4

2

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]
Sign up to request clarification or add additional context in comments.

6 Comments

Maybe the OP should stick to basics if they cannot do a simple for-loop.
Although this solution works, it is probably not the best solution for a beginner as through the eyes of a beginner they probably can't understand what is happening in that line.
I can certainly understand the sentiments. A case might be made that it actually avoids the indentation pratfall that happened in the question and is the more direct way to accomplish the goal.
You might as well at least explain the key issue here, i.e. iterating directly over the list.
Im a week into learning python in my spare time, so go easy please.
|
2

The cause of this is at the else: statement at the bottom, you haven't indented the line new_list = new_list + ['N'] so it is being ran no matter the result.

There are also a few other improvements which I've made and added comments explaining what it's doing

Change your code to this:

temperatures = [39.3, 38.2, 38.1]

new_list = []

for temperature in temperatures: #goes through all the values in "temperatures" and assigns it to the variable "temperature"
    if temperature < 38.3: #if temperature is less than 38.3
        new_list.append('L') #append (add) 'L' to the list
    elif temperature > 39.2: #if temperature is greater than 39.2
        new_list.append('H') #append 'H' to the list
    else: #else temperature is greater than or equal to 38.3 or less than or equal to 39.2
        new_list.append('N') #append 'N' to the list

Comments

1

The issue is with the indentation of the line new_list = new_list + ['N']. Because it is under-indented, it runs for every instance.

If I can suggest an easier syntax:

temperatures = [39.3, 38.2, 38.1]

new_list = []
for temperature in temperatures:
    if temperature < 38.3:
        new_list.append('L')
    elif temperature > 39.2:
        new_list.append('H')
    else:
        new_list.append('N')

print(new_list)
print(len(new_list))

->

['H', 'L', 'L']
3

Comments

0

Hope it will solve your issue:

tempretures = [39.3, 38.2, 38.1]
new_list = []
for temperature in tempretures:
    if temperature > 39.2:
        new_list.append('H')
    elif temperature>=38.3 and temperature<39.2:
        new_list.append('N')
    else:
        new_list.append('L')

print (new_list)

sample output: ['H', 'L', 'L']

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.