Header Ads

Responsive Ads Here

Iterator vs Iterable

 Iterator vs Iterable



l = [1,2,3,4,5] # iterable
print(l)
x = map(lambda a: a ** 2, l)  # iterator
print(x)

# for i in x:
#     print(i)
print(next(x))
print(next(x))
print(next(x))
print(next(x))
print(next(x))

# iterable ---> user can iterate over
# iterator ---> helps user to iterate over another object(iterables)


# How for loop works?
for i in l:
    print(i)


my_iter = iter(l)
print(my_iter)

print(next(my_iter))
print(next(my_iter))
print(next(my_iter))
print(next(my_iter))
print(next(my_iter))

Powered by Blogger.