Profile

Click to view full profile
Hi, I'm Veerapat Sriarunrungrueang, an expert in technology field, especially full stack web development and performance testing.This is my coding diary. I usually develop and keep code snippets or some tricks, and update to this diary when I have time. Nowadays, I've been giving counsel to many well-known firms in Thailand.
view more...

Monday, November 1, 2010

Differences between xrange() and range() functions in Python

Those 2 functions purpose is very similar when you use them in python, but the major different is range() function will generate the new list but xrange() is not. So, if your purpose doesn't use the advantage of the list to access you can just use xrange() function to make your code use less resources. For example, for-loop statement you can just use xrange() function to enumerate your loop.

for x in xrange(5):
 print x

Even you change xrange() function to range() function the result will be the same as:
0
1
2
3
4

But, As I already said, behind the scene range() function will generate it as a list that may make it slow when generate the big list for normal enumerate, xrange() function is enough.

No comments:

Post a Comment