+ 1

Python

Can you please explain me, how this code works? n=[1,2,3,4,5,6] for i, item in enumerate(n): n[i]*=2 print(n) I can’t understand, why and how we use “item” and “enumerate”

27th Dec 2021, 7:33 PM
Timofey Ivanenko
Timofey Ivanenko - avatar
3 odpowiedzi
+ 1
"i" stores a number, the index And "item" stores a value, the item https://www.w3schools.com/JUMP_LINK__&&__python__&&__JUMP_LINK/ref_func_enumerate.asp
27th Dec 2021, 7:43 PM
CGM
CGM - avatar
27th Dec 2021, 7:44 PM
Timofey Ivanenko
Timofey Ivanenko - avatar
+ 1
I don't know what it would be used for in a list, but in that attached page it uses it for a tuple, which makes more sense since they are not indexed
27th Dec 2021, 7:46 PM
CGM
CGM - avatar