Python-can anybody pls xplain this | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Python-can anybody pls xplain this

ls=[2,4,1,"bars",0,"foo",9] it=iter(ls[1:]) print(next(it),end="0") next(it) next(it) print(it.__next__())

6th Oct 2018, 1:54 AM
AMAN TOMAR
AMAN TOMAR - avatar
3 Answers
+ 6
In the second line, an iterator is created that contains the elements [1:] of the list (4, 1, 'bars', 0, 'foo', and 9). Both next(it) and it.__next__() call the next item of the iterator. In line 3, the first item (4) is called and printed and '0' is appended. In lines 4 and 5, the next items (1 and 'bars') are called, but nothing happens with them (especially they are not printed), so they are just lost. In the last line, the next element (0) is printed, so the whole result is '400'.
6th Oct 2018, 5:26 AM
Anna
Anna - avatar
+ 2
You're welcome
6th Oct 2018, 5:33 AM
Anna
Anna - avatar
+ 1
thank you
6th Oct 2018, 5:33 AM
AMAN TOMAR
AMAN TOMAR - avatar