Wednesday, July 11, 2007

iter() sample

the iter() is more like generator, it will return an item every time you call it. here is the different output by using list and iter()

it = range(10)
print it
b = zip(it, it)
print b

it = iter(range(10))
b = zip(it, it)
print b

[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
[(0, 0), (1, 1), (2, 2), (3, 3), (4, 4), (5, 5), (6, 6), (7, 7), (8, 8), (9, 9)]
[(0, 1), (2, 3), (4, 5), (6, 7), (8, 9)]

No comments: