0

If I have a file iterator

with open('test1.txt','r') as f1:
 print(f1.__next__())

But if I do the same thing for a list, it doesn't work.

a1 = [1,2,3,4,5]
a1.__next__()

So, what is difference between file iterator and list iterator? Do file and list (or tuple, dictionary, etc.) iterators behave differently?

bner341
  • 525
  • 1
  • 7
  • 8
  • 2
    BTW, you shouldn't call the `__next__` method directly, please use the `next` function, eg `next(f1)`. The same goes for most other "magic" methods whose names begin & end with double underscores (aka dunder methods). So use the `len` function, not the `__len__` method, the `str` function, not the `__str__` method, etc. – PM 2Ring Aug 20 '17 at 17:10

1 Answers1

1

There is no such file iterator and list iterator.Iterator works on iter objects. List themselves are iterable , but not an iter object, however we can make them iterable.

a =[1,2,3,4,5]
a= iter(a)
a.next()

All python data types except number can be made iterable.

Confused with python lists: are they or are they not iterators?

SarathSprakash
  • 4,614
  • 2
  • 19
  • 35