python-cpp for loop

How to write the below python for loop

1
2
  //Here the obj is the list of objects
  for i, j in zip(obj[:-1], obj[1:]):


Tank you
You have to explain the purpose of "zip" and "indices".
Roughly
obj = [10,20,30,40,50]
for i, j in zip(obj[:-1], obj[1:]):
    print( i, j )

Cut and paste into https://www.w3schools.com/python/trypython.asp?filename=demo_compiler



1
2
3
4
5
6
7
8
9
10
#include <iostream>

int main()
{
   int obj[] = { 10, 20, 30, 40, 50 };
   int N = sizeof obj / sizeof obj[0];

   for ( int i = 0, j = 1; i < N - 1; i++, j++ )
      std::cout << obj[i] << ' ' << obj[j] << '\n';
}
Last edited on
Consecutive elements? In that case:
1
2
3
4
5
6
7
8
9
10
11
12
#include <iostream>

int main()
{
   int obj[] = { 10, 20, 30, 40, 50 };
   int N = sizeof obj / sizeof obj[0];

   for ( int i = 0; i+1 < N; ++i ) {
      // j == i+1
      std::cout << obj[i] << ' ' << obj[i+1] << '\n';
  }
}
Topic archived. No new replies allowed.