+ 2

Why is iter() needed here?

code: l = [1, 2, 3, 4] l = iter(l) for i in l: print(i, next(l)) Why do you have to call iter() when the for loop already has done that?

11th Feb 2019, 4:27 PM
Juho Pesonen
Juho Pesonen - avatar
2 Réponses
+ 9
For behind the scenes use iter so in that scenario in one loop you process two values.
11th Feb 2019, 4:55 PM
Hubert Dudek
Hubert Dudek - avatar
+ 4
For fun ;-)
11th Feb 2019, 4:51 PM
Hubert Dudek
Hubert Dudek - avatar