How to skip index in python for loop

I have a list like this:

array=['for','loop','in','python']

for arr in array:
    print arr

It will give me a way out

for
lop
in
python

I want to print

in
python

How can I skip the first 2 indexes in python?

+4
source share
5 answers

Code:

array=['for','loop','in','python']
for arr in array[2:]:
    print arr

Output:

in
python
+4
source

Use slicing .

array = ['for','loop','in','python']

for arr in array[2:]:
    print arr

When you do this, the starting index in the loop forwill become 2. Thus, the output will be:

in
python

For more information, slicingread the following: Explain Python fragment notation

+8
source

. Python:

for arr in array[2:]:
    print arr
+2

,

:

for i ,j in enumerate(array):
    if i not in (1,2):
        print j

:

for
python
+2

, , itertools.islice:

import itertools
array = 'for loop in python'.split()
for x in itertools.islice(array, 2, None):
    print x

because the slice statement creates a completely new list, which means overhead if you just want to iterate over it.

+2
source

Source: https://habr.com/ru/post/1606589/


All Articles