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?

1
  • 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. Commented Aug 20, 2017 at 17:10

1 Answer 1

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?

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.