duplicated values in print()
Question:
Can anyone tell me why im getting duplicated values when I print the results of this list?
degrees_values = [0, 5.85, -2.5]
kelvin_values = []
for degrees in degrees_values:
kelvin = degrees + 273.15
kelvin_values = kelvin_values + [kelvin]
print(kelvin_values)
I’ve tried printing the list length, just ‘kelvin’ but this isn’t the result I’m after. I’m consantly seeing this:
[273.15]
[273.15, 279.0]
[273.15, 279.0, 270.65]
when im looking for just:
[273.15, 279.0, 270.65]
Answers:
You should move the print
statement out of the for
loop block as such:
degrees_values = [0, 5.85, -2.5]
kelvin_values = []
for degrees in degrees_values:
kelvin = degrees + 273.15
kelvin_values = kelvin_values + [kelvin]
print(kelvin_values)
When you put the print
inside the block, each iteration you print kelvin_values
and it "spams" your console..
This is an indentation problem.
It seems like you did not paste your code correctly in StackOverflow, but I assume that it looks like this :
degrees_values = [0, 5.85, -2.5]
kelvin_values = []
for degrees in degrees_values:
kelvin = degrees + 273.15
kelvin_values = kelvin_values + [kelvin]
print(kelvin_values)
What you are seeing is the list being built, because there is a print statement in each iteration.
I suggest you move the print statement out of the for loop so you only see the final result :
degrees_values = [0, 5.85, -2.5]
kelvin_values = []
for degrees in degrees_values:
kelvin = degrees + 273.15
kelvin_values = kelvin_values + [kelvin]
# I do not indent here
print(kelvin_values)
Moreover, it could be wiser to use the append method of the list to add values like so :
degrees_values = [0, 5.85, -2.5]
kelvin_values = []
for degrees in degrees_values:
kelvin = degrees + 273.15
kelvin_values.append(kelvin)
print(kelvin_values)
Can anyone tell me why im getting duplicated values when I print the results of this list?
degrees_values = [0, 5.85, -2.5]
kelvin_values = []
for degrees in degrees_values:
kelvin = degrees + 273.15
kelvin_values = kelvin_values + [kelvin]
print(kelvin_values)
I’ve tried printing the list length, just ‘kelvin’ but this isn’t the result I’m after. I’m consantly seeing this:
[273.15]
[273.15, 279.0]
[273.15, 279.0, 270.65]
when im looking for just:
[273.15, 279.0, 270.65]
You should move the print
statement out of the for
loop block as such:
degrees_values = [0, 5.85, -2.5]
kelvin_values = []
for degrees in degrees_values:
kelvin = degrees + 273.15
kelvin_values = kelvin_values + [kelvin]
print(kelvin_values)
When you put the print
inside the block, each iteration you print kelvin_values
and it "spams" your console..
This is an indentation problem.
It seems like you did not paste your code correctly in StackOverflow, but I assume that it looks like this :
degrees_values = [0, 5.85, -2.5]
kelvin_values = []
for degrees in degrees_values:
kelvin = degrees + 273.15
kelvin_values = kelvin_values + [kelvin]
print(kelvin_values)
What you are seeing is the list being built, because there is a print statement in each iteration.
I suggest you move the print statement out of the for loop so you only see the final result :
degrees_values = [0, 5.85, -2.5]
kelvin_values = []
for degrees in degrees_values:
kelvin = degrees + 273.15
kelvin_values = kelvin_values + [kelvin]
# I do not indent here
print(kelvin_values)
Moreover, it could be wiser to use the append method of the list to add values like so :
degrees_values = [0, 5.85, -2.5]
kelvin_values = []
for degrees in degrees_values:
kelvin = degrees + 273.15
kelvin_values.append(kelvin)
print(kelvin_values)