-3

Is there a one-line way to translate this "for" loop from C++ to python?

for(int i = 0; i < = 10 && i != 6; i++)

I mean in C++ we have :

for(initial_value; condition(s); step)

and I am looking for the analogue version in Python where we can impose one or several conditions.

ChesuCR
  • 9,352
  • 5
  • 51
  • 114
Artashes
  • 102
  • 1
  • 9
  • 1
    Not clear what you want. In particular, the C++ code can be simplified: `for(int i = 0; i!= 6; i++)` – Damien Oct 20 '20 at 09:40
  • Unless `i` gets modified inside the loop? – Thierry Lathuille Oct 20 '20 at 09:41
  • @mch my bad, you are correct – bhucho Oct 20 '20 at 09:53
  • 1
    Python's range function also uses a half-open range, just like it's customary for C++ loops, @mch. – Ulrich Eckhardt Oct 20 '20 at 10:11
  • @UlrichEckhardt you are right, so it is `for i in range(0, 6):` – mch Oct 20 '20 at 10:26
  • @Damien, yes how will you translate for(int i = 0; i!= 6; i++) in python ? '''''' – Artashes Oct 21 '20 at 08:28
  • @Artashes I am not a Python expert, but your C++ code is equivalent to `for(int i = 0; i < 6; i++)`. Therefore, I guess something like `for i in range(6):`. Maybe your C++ code doest not do what you are thinking it does – Damien Oct 21 '20 at 08:33
  • @Artashes I know C++ and I saw that your initial C++ code can be greatly simplified, at least assuming that this code is effectively doing what you want. You did not explain what this code is supposed to do. My first comment on C++ is correct, what not giving it? And I don't need to be a Python expert to know the Python translation of the simplified C++ code. So, what is the problem? – Damien Oct 21 '20 at 09:09
  • dear @damien this is not the place to debate, your for loop suggerstion is NOT the équivaent of the one I poste. – Artashes Oct 21 '20 at 11:58
  • Maybe it would be best to post a full program, which prints messages, and demonstrate which output you are trying to get. You can [edit] your post if you want to clarify it. However, you got your answer, so do it only if you care about whoever will read this in future. – anatolyg Oct 21 '20 at 15:25

3 Answers3

2

In C++ a loop like

for (a; b; c)
   d;

is equivalent to

{
    a;
    while (b)
    {
        d;
        c;
    }
}

If you first translate your for loop to its corresponding while loop, then you could translate that while loop to Python.

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
1

Probably the simplest Pythonic articulation is

for i in range(11):
    if i == 6:
        continue
    ... stuff happens ...
tripleee
  • 175,061
  • 34
  • 275
  • 318
  • This is exactly how I've done, I was juste curious if there exist one-line simple way as in C++ – Artashes Oct 21 '20 at 08:07
  • Maybe see https://stackoverflow.com/questions/5805914/discontinuous-slice-in-python-list for some creative variations. – tripleee Oct 21 '20 at 08:12
1

If you want to express the idea "do something for numbers from 0 to 10, except 6", the correct C++ code for it cannot be a simple for-loop:

for (int i = 0; i <= 10; i++)
{
    if (i != 6)
    {
        ... // do the stuff
    }
}

You can translate it literally to Python (see answer by tripleee) or make a real one-liner, like so:

Create a list of numbers for which you want to run the stuff: [x for x in range(11) if x != 6]. Then iterate on it:

for i in [x for x in range(11) if x != 6]:
    ... # do the stuff

From code readability standpoint, the solution with if or continue may be better than this one.

anatolyg
  • 26,506
  • 9
  • 60
  • 134