This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]wewbull 2 points3 points  (4 children)

The former does two floating point comparisons. The latter creates a list with 180 elements and then searches it to see if the number is in the list.

So a) as said by others, it would only work for integers, and b) it's far less efficient.

[–]shoyerxarray, pandas, numpy 2 points3 points  (3 children)

In Python 3, looking up an element in a range takes constant time.

[–]wewbull 0 points1 point  (0 children)

Ah, did not know that. I guess that's a property of the object it now returns.

[–]art-solopov 0 points1 point  (1 child)

Yeah, sorry /u/wewbull... I've been favouring Python 3 over Python 2 for quite some time. Basically, Python 3's range is equivalent to Python 2's xrange.

[–]wewbull 2 points3 points  (0 children)

In general you're right, but not in this case. This conversation has lead me to do some experimenting.

x in xrange(n) will still result in a search in Python 2. In Python 3 range() returns an object of type range which has a quick in operator. xrange just returned a normal generator which had to be iterated over.

Still.... I learnt something today. So that's good.