Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to skip the index in python for loop

I have a list like this:

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

for arr in array:
    print arr

This will give me the output

for
lop
in
python

I want to print

in
python

How can I skip the first 2 indices in python?

like image 386
Bhavik Joshi Avatar asked Sep 09 '15 13:09

Bhavik Joshi


1 Answers

Use slicing.

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

for arr in array[2:]:
    print arr

When you do this, the starting index in the for loop becomes 2. Thus the output would be:

in
python

For more info on slicing read this: Explain Python's slice notation

like image 57
Indradhanush Gupta Avatar answered Oct 09 '22 12:10

Indradhanush Gupta