EDIT 3: The answer by @hynekcer is much much better than this.
EDIT 2: This will not work if you have an infinite iterator, or one which consumes too many Gigabytes (in 2010 1 Gigabyte is still a large amount of ram/ disk space) of RAM/disk space.
You have already seen a good answer, but here is an expensive hack that you can use if you want to eat a cake and have it too :) The trick is that we have to clone the cake, and when you are done eating, we put it back into the same box. Remember, when you iterate over the iterator, it usually becomes empty, or at least loses previously returned values.
>>> def getIterLength(iterator):
temp = list(iterator)
result = len(temp)
iterator = iter(temp)
return result
>>>
>>> f = xrange(20)
>>> f
xrange(20)
>>>
>>> x = getIterLength(f)
>>> x
20
>>> f
xrange(20)
>>>
EDIT: Here is a safer version, but using it still requires some discipline. It does not feel quite Pythonic. You would get the best solution if you posted the whole relevant code sample that you are trying to implement.
>>> def getIterLenAndIter(iterator):
temp = list(iterator)
return len(temp), iter(temp)
>>> f = iter([1,2,3,7,8,9])
>>> f
<listiterator object at 0x02782890>
>>> l, f = getIterLenAndIter(f)
>>>
>>> l
6
>>> f
<listiterator object at 0x02782610>
>>>