I have the following problem:
I have a list:
temp= [950, 1000, 1100, 1200, 1400, 1450, 1500, 1600, 1650, 1700, 1900, 1950, 2000, 2100, 2200]
in this list I want to remove every value that is < 1200 and > 1950
.
I tried the following:
for x in temp:
if x < 1200 or x > 1950:
temp.remove(x)
That gives me the following result:
[1000, 1200, 1400, 1450, 1500, 1600, 1650, 1700, 1900, 1950, 2100]
But the output I am aiming for would be the following:
[1200, 1400, 1450, 1500, 1600, 1650, 1700, 1900, 1950]
I found a way to complete my task with slicing:
new_temp = temp[temp.index(1200):temp.index(1950)+1]
That gives me the output I want:
[1200, 1400, 1450, 1500, 1600, 1650, 1700, 1900, 1950]
But I would like to understand why attemp1 doesn´t work and if maybe there are better ways to fulfil my task than attemp2
Can anyone help? Thanks in advance.