Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Starting from a specific point in a For loop

Tags:

python

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.

like image 414
Nate Avatar asked Jan 09 '17 19:01

Nate


People also ask

How do you make a loop start from 1?

Use n+1 in Place of n in the range() Function to Start the for Loop at an Index 1 in Python. This method can be implemented by using the start value as 1 and the stop value as n+1 instead of default values 0 and n , respectively.

Does a for loop start at 0 or 1?

The reason a lot of for loops start at 0 is because they're looping over arrays, and in most languages (including JavaScript) arrays start at index 0."

How do you loop to the beginning of a list in Python?

We can loop back to the start by using a control flow statement, i.e., a while statement. To do that, wrap the complete program in a while loop that is always True. What is this? Moreover, add a continue statement at a point where you want to start the program from the beginning.


5 Answers

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

like image 127
chickity china chinese chicken Avatar answered Oct 17 '22 08:10

chickity china chinese chicken


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.

like image 30
snakecharmerb Avatar answered Oct 17 '22 08:10

snakecharmerb


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
like image 24
Ari Gold Avatar answered Oct 17 '22 06:10

Ari Gold


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
like image 22
shiva Avatar answered Oct 17 '22 08:10

shiva


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
like image 34
Harsha Biyani Avatar answered Oct 17 '22 06:10

Harsha Biyani