Complete Guide to use Loop, xrange and Generator functions in Python

Arnab M Last Updated : 18 Aug, 2021
6 min read
This article was published as a part of the Data Science Blogathon

Introduction :

Looping is a basic concept in any programming language and can be used in a variety of daily uses. When you have a huge list or you need to process all the data fields of the dataset one by one, you can sequentially loop over or iterate over the whole set of data. There are two types of looping statements in Python which are  :

  • For Loop
  • While Loop
loop xrange and generator

Source: https://unsplash.com/photos/u2d0BPZFXOY

We will discuss them one by one in the following paragraphs :

For Loop :

For loop in Python is nothing similar to C or Java and is more closely related with the ” foreach ” keyword in C++. We normally use “for variable in” to either iterate over a whole list of values.

Syntax :

for iterator_variable in iterator_list :

Eg Code :

for i in range(5):
  print(i)

Output :

0
1
2
3
4

Here we have used a range function that takes an integer value as input and returns a list of numbers that begin at 0 and end at the number supplied but does not include the value passed. In this example, we had passed the value 5 but the values returned by the range function will vary from 0 to 4 and will not include 5. This is very helpful and intuitive since, in coding languages where we often use lists, arrays and other sequential data structures, indexing always begins at 0 and goes to the length-1 value.

Xrange :

We can also use “xrange()” instead of “range()” as both return the same set of values but the working of them are different and there exist some slight variations between them which we will revisit later in this article.

The areas of difference for xrange and range are :

  • Return Type: Range returns a list but xrange returns an xrange object.
  • Memory: Range utilizes more memory than xrange as range creates the whole list at once but xrange creates the value required at execution and is also known as “lazy evaluation“. So instead of returning the whole list, it only returns the value needed for execution and is more than 95% space-efficient than range.
  • Operations: We can use list functions on range values but not on the xrange object since it does not return a list.
  • Speed: The speed of xrange is much faster than range due to the “lazy evaluation” functionality.

Important Note: If you want your code to be both Python 2 and Python 3 compatible, then you should use range as xrange is not present in Python 3, and the range of Python 3 works in the same way as xrange of Python 2.

For Loop Usage :

We can use a for loop to iterate over a range or even traverse a string, one character at a time. Basically, we can iterate over something which has multiple values stored in a particular fashion and can be indexed. This list includes a list, tuples, set, and a dictionary too.

Code :

for n in L:
  print(n)

Here L can be a list of values and then n will traverse over the whole list one by one and then we can extract and access all values of the iterator one by one.

if L = [1,2,3,4,5]      #List or tuples

Output :

1
2
3
4
5

If L = “Analytics”     # String

Output :

A
n
a
l
y
t
i
c
s

if L = {1:”a”,2:”b”,3:”c”}     #Dictionary

Output :

1
2
3

if we want the value along with the index of the dictionary, we can edit the code to become like the following :

Code :

for n in L:
  print(n,L[n])

Output :

1 a
2 b
3 c

Note: We can also use the dict.items() functions to iterate over a dictionary with a (key,value) tuple paid.

The range function takes 3 parameters actually which are the start, end and the step counter.

Eg #1x=range(0,10,2) will give a list of [0,2,4,6,8] which begins at 0 and goes to 10, exluding 10 and increases by 2 steps. Its C/C++/Java equivalent is for (i=0,i<10;i+=2){}

Eg #2: x=range(10,0,-2) will give a list of [10,8,6,4,2] which begins at 10 and goes to 0 but does not include 0 and decreases by 2 steps. Its C/C++/Java equivalent is for (i=10;i>0;i-=2){}

While Loop :

A while loop only takes the exit condition after the while keyword and executes the statements in its block until the condition becomes false. It is generally used when one person does not know how many times the loop will get executed.

Syntax : 

while condition :

Code :

i=0
while i<5:
  print(i)
  i=i+2

Output :

0
2
4

Note: There are no Do-While loops in Python but you can make one of your own.

Bonus Looping Notes : 

The execution control can be controlled by the following three keywords which are :

  • Pass
  • Break
  • Continue

Break means that the execution will stop and exit the for loop, Continue will force the loop to continue onto the next iteration and ignore all the following lines of execution. Pass will simply do nothing and is often used for empty functions or loops.

Code :

s = "analytics"
# Pass usage
for i in s:
	if i == 'a':
		print('Pass has been executed')
		pass
	print(i)
        if i == 't':
                break
print(":: Now continue ::")
# Continue usage
for i in s:
	if i == 'a':
		print('Continue has been executed')
		continue
	print(i)
         if i == 't':
                  break

Output :

Pass has been executed
a
n
Pass has been executed
a
l
y
:: Now continue ::
Continue has been executed
n
Continue has been executed
l
y

This example clearly shows the difference between a pass and continue and that wraps it up for loops and now we explore generator functions Python.

You can use Else block with Loops!

Did you know that you can use else block or the keyword with the for or while loop statements. When a while or a for block finishes its execution, the else block is executed and then the flow of execution moves outside towards the outer block.

Code :

i=0
while i<10:
  print(i)
  i=i+1
else:
  print("End",i)
print("In main Indent")

Output :

0
1
2
3
4
5
6
7
8
9
End 10
In main Indent

The else block will not be executed in case if there was a break statement executed in the loop block and in that case, the execution will move back to the main block. This flow can be better explained by the following

Code :

i=0
while i<10:
  print(i)
  i=i+1
  if i==6:
    break
else:
  print("End",i)
print("In main Indent")

Output :

0
1
2
3
4
5
In main Indent

The same can be used with for loops in python too which is shown by the following code and output

Code :

for i in range(5):
  print(i)
else:
  print("End",i)

Output :

0
1
2
3
4
End 4

Here you can see that the last value is 4 and not 5 like the while loop because the range function returns a list of values that begins at 0 and ends at 4, so the last value fo i is 4 and it does not get updated to 5 to exit out of the loop but it simply ends at 4 when it reaches the end of the list to traverse.

Generator Function In Python :

We can create our own generator function where we use yield instead of the return function. The yield function pauses the execution and saves the execution variables and states and we can resume execution by using the “next” keyword.

Code :

def TestGenerator():
    yield 1            
    yield 2            
    yield 3            
for valin TestGenerator(): 
    print(val)

Output :

1
2
3

A generator returns a generator object which you can also use like this :

Code :

def TestGenerator():
    yield 1
    yield 2
    yield 3
# x is a generator object
x = TestGenerator()
print(x.next()) # __next__() in Python 3.x
print(x.next())
print(x.next())

Output :

1
2
3

Generator Expression :

Python provides an easy way to use Python Generator expressions where we can write the yield and range in one single line like this :

Code :

s = (x+x for x in range(5))
print(next(s))
print(next(s))
print(next(s))
print(next(s))
print(next(s))

Output :

0
2
4
6
8

We can also use list expressions like this and declare a list of squares with :

Code :

s= [ x*x for x in range(5) ]
print(s)

Output :

[0,1,4,9,16]

This is an example of a list generator and let’s see the benefits of using a generator :

Applications :

Suppose we have a constant stream of data inflow into our code and using a generator helps us to work efficiently with it. Without waiting for the full list of values, we can use the next keyword to work on the stream of data one by one as it is ingested. It is also useful while handling large files of data like log files and this provides a space-efficient function for processing such data for handling the data in parts. Iterators can also handle the same task but generators are more efficient.

End Notes :

So now you know how to effectively use Looping statements in Python along with generators and the difference between range and xrange. I hope this article helps you in any way possible and keep coding.

Stay Safe and get vaccinated everyone.

About the Author :

Arnab Mondal (LinkednIn)

Data Engineer | Python, C/C++, AWS Developer | Freelance Tech Writer

Link to my other articles

The media shown in this article are not owned by Analytics Vidhya and are used at the Author’s discretion.

I love to code and create new software for any purpose. I also love to play MMO and RTG games. Other hobbies include Exploring new places and restaurants and making new friends. Feel free to ping me on LinkedIn for any new ideas or same and if you need any help with any code too.

Responses From Readers

Clear

Congratulations, You Did It!
Well Done on Completing Your Learning Journey. Stay curious and keep exploring!

We use cookies essential for this site to function well. Please click to help us improve its usefulness with additional cookies. Learn about our use of cookies in our Privacy Policy & Cookies Policy.

Show details