Python : range() Vs xrange()

Both do the same job, difference is in their return type and memory type, execute below code and experience

r = range(0,19)
x = xrange(0,19)
print type(r)
print type(x)

Output :
<type ‘list’>
<type ‘xrange’>

range generates the sequence of number as list  which consumes actual memory, however xrange() is just a generator which does not saves the entire range of numbers in memory as list.

Note : In python 3 range is same as xrange.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s