0

So I want to execute only while loop statements, without putting anything inside them. For example, I have an array arr from which I have to remove multiple occurrences of some element. The instant the condition statement returns an error, while loop should end.

arr=[1,2,4,2,4,2,2]

This removes only one 2:

arr.remove(2)

I need to run this as long as it does not return error. (C++ has a semicolon put after while to do this). I want something like this

while(arr.remove(2));
kramer
  • 849
  • 2
  • 10
  • 19
  • Is there any reason you are not opting to use a list comprehension for this? It would be more idiomatic to use a list comprehension rather than this. –  Apr 08 '20 at 17:45
  • 1
    Please post what you’ve tried so far, and where you are getting stuck. – S3DEV Apr 08 '20 at 17:46
  • 4
    The `remove` method always returns `None`, but you could build something with a `while True` loop and catching the `ValueError`. The "Python way" would be to build a new list as @SilverNitrateIon already said. – Matthias Apr 08 '20 at 17:48
  • in Python you use `pass` to create empty loop, function, class, ie. `while True: pass` – furas Apr 08 '20 at 17:49
  • 1
    I solved the problem using list comprehension but was wondering if there was a way to do this. – kramer Apr 08 '20 at 17:49
  • I understand that I can't use this method for `remove` – kramer Apr 08 '20 at 17:53

5 Answers5

2

The way you are looking to solve this does not yield the results you are looking for. Since you are looking to create a new list, you are not going to want to use the remove function as per @Matthias comment. The idiomatic way to do it would be something along the lines of this:


arr=[1,2,4,2,4,2,2]
arr = [x if x != 2 for x in arr]

2

Three things.

First, it's not considered good practice in Python – it's not "pythonic" – to use an expression for its side effects. This is why, for example, the Python assignment operator is not itself an expression. (Although you can do something like a = b = 1 to set multiple variables to the same value, that statement doesn't break down as a = (b = 1); any such attempt to use an assignment statement as a value is a syntax error.)

Second, modifying data in place is also discouraged; it's usually better to make a copy and make the changes as the copy is constructed.

Third, even if this were a good way to do things, it wouldn't work in this case. When the remove method succeeds, it returns None, which is not truthy, so your loop exits immediately. On the other hand, when it fails, instead of returning a false value, it throws an exception, which will abort your whole program instead of just the loop, unless you wrap it in a try block.

So the list comprehension probably is the best solution here.

Mark Reed
  • 91,912
  • 16
  • 138
  • 175
1

So I want to execute only while loop statements, without putting anything inside them.

That's really not necessary. Don't try to copy other language's syntax in Python. Different languages are designed with different objectives and hence, they have different syntax (or grammar of the language). Python has a different way of doing things than C++.

If you want to focus on the effectiveness of the program, then that's the different story. See this for more information on this.

Unfortunately, remove doesn't return anything (it returns None). So, you can't have anything that would look neat and clean without putting anything inside while.

Pythonic way to remove all occurrence of a element from list:

list(filter((2).__ne__, arr))

Or

arr = [x for x in arr if x != 2]

Or

while 2 in arr:
    arr.remove(2)
abhiarora
  • 9,743
  • 5
  • 32
  • 57
0

you can use:

arr = [1,2,4,2,4,2,2]

try:
    while arr.pop(arr.index(2)):
          pass
except ValueError:
    pass

print(arr)
#[1, 4, 4]
kederrac
  • 16,819
  • 6
  • 32
  • 55
0

I am assuming you want to remove all occurrences of an element. This link might help you. click here