Click here to Skip to main content
15,878,852 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
lst = [10, 11, 34, 56, 23]

for x in lst:
    if x > 10 or x < 34:
        lst.remove(x)

print(lst)


What I have tried:

i tried to reverse x > 34 but i kept getting non logical like
[10, 34, 23]
Posted
Updated 11-Sep-21 9:45am

Think about the expression in your if statement. You have to evaluate each expression to the left and right of the or operator separately, then apply the or operator to the results of those expressions.

So, on the first number in the list, you get:
C#
    if x > 10 or x < 34

plug in the 10 for x:

    if (10) > 10 or (10) < 34

and evaluate each expression:

    if   false   or   true

The result of (False or True) is True

Do it again for your remaining numbers and, hopefully, you begin to see why that or operator is not appropriate and needs to be replaced with something else.

Oh, and 10 < 10 is FALSE, not true. If you want that to be true, you have to change the > to >=
 
Share this answer
 
v2
Comments
Member 15331575 11-Sep-21 13:59pm    
ok , but what about 11 isn't it true or true , shouldn't it also be removed and what about 56 it is true or false so true and should be removed ? thank you
Dave Kreskowiak 11-Sep-21 16:30pm    
It's because you cannot modify the collection while you are iterating over it.

Read the documentation on the for statement: 4. More Control Flow Tools — Python 3.9.7 documentation[^]
Two things are going on here:

for every x in lst the expression (x > 10 or x < 34) is true, so you might expect that you would get an empty list at the end, except that the statement for x in lst iterates over the list - that is it is equivalent of
i = 0
while i < lst.length()
   if(x > 10 or x < 34)
       list.remove(x)
   i += 1;

In the first path, lst.length() = 5, i = 0, x = 10. x is less than 34, so we call list.remove(10), and then increment i. What we now have is
lst=[11, 34, 53, 23]
     i = 1
For the second pass, we remove 34, since we are now examining lst[1], and so on, removing 23 on the next pass. So we end up with lst = [11, 53]
 
Share this answer
 
Think about it: what numbers aren't greater than 10 or less then 34?
If its less than 10, it's less than 34, and if it's less than 34 it's also greater than 10!

I suspect you mean to say and rather than or:
if x > 10 and x < 34:
Meaning it's between them!

The other problem is that you are modifying the collection on which you are looping inside the loop - which messes up the for iterator.
In a sensible language you get an error when you try to do that, but nobody ever said python is a sensible language!
Each time you remove an element from the collection, you "skip" the next entry when you go back round the loop.

Instead of removing values, create a second list and add the one you want to that. Then print that collection after the loop has exited.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900