7

Is it possible to tell a For loop to start from a specific location?

>>> languages = ["C", "C++", "Perl", "Python"] 
>>> for x in languages:
...     print(x)
... 
C
C++
Perl
Python
>>> 

How can I get the script to begin from "Perl"? I don't need it to loop back around.

Nate
  • 91
  • 1
  • 2
  • 3
  • 1
    By slicing the list and iterating through that – Andrew Li Jan 09 '17 at 19:07
  • [see also](http://stackoverflow.com/questions/176918/finding-the-index-of-an-item-given-a-list-containing-it-in-python) – user25064 Jan 09 '17 at 19:08
  • Possible duplicate of [pythonic way to iterate over part of a list](http://stackoverflow.com/questions/8671280/pythonic-way-to-iterate-over-part-of-a-list) – SiHa Jan 09 '17 at 19:21

6 Answers6

7

Lists can be sliced by their indices using myList[start:stop:skip]. Starting from the third index (zero-indexing), no stopping, and no skipping of indices, would be:

for x in languages[2::]:
    print x

would give you:

Perl
Python

See also: Explain Python's slice notation

Community
  • 1
  • 1
3

You could use itertools.dropwhile for this:

>>> import itertools

>>> languages = ["C", "C++", "Perl", "Python"] 

>>> for language in itertools.dropwhile(lambda x: x != 'Perl', languages):
...     print(language)
... 
Perl
Python
>>> 

itertools.dropwhile takes two arguments, a function that determines whether a list element is equal to your desired starting value, and the iterable - a list in your case - that you want to loop over. dropwhile will skip elements until it finds one that satisfies the function, then it will output all elements until the iterable is exhausted.

This is useful if your criteria for selecting a start point are more complex than simple equality. For example, if you wanted to start from the first element that starts with "P" you can do:

>>> for language in itertools.dropwhile(lambda x: not x.startswith('P'), languages):
...     print(language)
... 
Perl
Python
>>> 

The reverse of this is itertools.takewhile, which will iterate for as long as the provided function is satisfied.

snakecharmerb
  • 47,570
  • 11
  • 100
  • 153
2
languages = ["C", "C++", "Perl", "Python"]
# add start index range by the index of "Perl" 
for x in languages[languages.index("Perl"):]:
  print(x)

# Perl
# Python

for x in languages[languages.index("C++"):]:
  print(x)

# C+=
# Perl
# Python
Ari Gold
  • 1,528
  • 11
  • 18
2

Try this. I hope this will help u

languages = ["C", "C++", "Perl", "Python"]

for k in range(0,len(languages)):
  print(languages[k-2])

and the output is :

Perl
Python
C
C++

In python the for loop function is (for k in x(start,stop,step)). Here x is your list or string variable and

start : Indicates where the for loop should start and the index starts from zero.

stop : Indicates where the for loop ends. It takes up to (n-1) elements.

For example :

for k in range(0,100):

it gives output till 99 from zero and if u want output of 100. U should mention like this :

n = 100
for k in range(n+1):
    print k

the output is from 0 to 100. In this case for loop will take indexes (starts) from zero.

step : Indicates how many steps should do in for loop. By default step is one in for loop.

for example:

for k in range(0,10,1):
    print k
#output is 0,1,2,3,4,5,6,7,8,9


for k in range(0,10,2):
    print k
#output is 0,2,4,6,8

for k in range(0,10,3):
    print k
#output is 0,3,6,9
shiva
  • 5,083
  • 5
  • 23
  • 42
1

You can try :

languages = ["C", "C++", "Perl", "Python"]
for i in range(languages.index("Perl"),len(languages)):
    # 1st parameter of range will find Perl's index from list languages
    #2nd parameter - Length of list
    print languages[i]


Perl
Python
Harsha Biyani
  • 7,049
  • 9
  • 37
  • 61
0

If you want elements from start of perl to the end of list

languages = ["C", "C++", "Perl", "Python"]
print languages

Its very easy by list slicing

print languages[2:]

output :

['Perl', 'Python']

you can loop this list and get elements on separate line.

And Even if you want to get the loopback elements from perl to back-to-perl then also you can use list slicing

a = []
a.extend(languages[2:])
a.extend(languages[:2])
print a

output:

['Perl', 'Python', 'C', 'C++']
Shivkumar kondi
  • 6,458
  • 9
  • 31
  • 58