For loop including enumerate method(Python) | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 4

For loop including enumerate method(Python)

a = [2, 4, 6, 8] sum = 0 for p, n in enumerate(a, 2): sum += p print(sum) Output: 14 So I just played challenge here and now confused by that code. First of all, why is there a second element n? How does it affect this? I already found out that it doesn't really matter what is in the list (wether there are strings, interegers or floats), but the amount of them affect the result. If there was one more item in the list, the output would be 20. By the way, if I remove the n element, it returns the TypeError. Also how the enumerate() function works here, is very unusual to me. I didn't find any similar examples to that. When I tried to remove it, it returned ValueError: too many values to unpack (expected 2). I'll be happy for your respond. https://code.sololearn.com/c6xK0s6XK2n2/?ref=app

22nd Jul 2019, 8:21 AM
Soda The Coda
Soda The Coda - avatar
4 Answers
+ 2
Here we are using p and n, p is to store the actual value of the number in the list and n for iterating over the list , It sum up all the values one by one and stores in variable called sum, sum = 0 sum +=p In this code enumerate(a, 2), here 2 indicates index of a list from which index it has to start to sum up the values . Use 0 instead of index 2 in enumerate(a, 0) gives actual sum of all the numbers in list. As in your code we are using 2 in enumerate, it start to sum up values from index 2 is 6 later 8, sum = 0 sum += 6 Now sum is 6 sum += 8 Now sum is 14
22nd Jul 2019, 9:15 PM
Anil
+ 6
Regardless from the question about enumeration there are issues in the supplied code that lead to a wrong result: If i sum the values in list 'a' it results to 20, but result of the code is 14. 2 issues lead to this result: a = [2, 4, 6, 8] sum = 0 for p, n in enumerate(a): #enumerate(a,2): <-- 2 as a parameter for enumerate means counting for index started with2 #sum += p # <-- p is used to sum but this is the index but not the value sum += a[p] # <-- this is correct print(sum) So i can understand the confusion from Soda. So just some more information about using enumerate. for p, n in enumerate(a): does the following: the first variable 'p' will be filled in each iteration with an index like 0 for the first iteration, 1 for the second iteration ... the second variable 'n' will be filled in each iteration with an element from list 'a' like 2 for first iteration, 4 for second iteration ... now you can use p and n to do something during each iteration loop...
22nd Jul 2019, 11:04 AM
Lothar
Lothar - avatar
+ 5
It's good that you played with the code before reaching out for help. If you'd have just tried printing p and n in each iteration you'd get to know that 'p' is nothing but a counter whereas 'n' holds the elements from the iterable 'a' during each iteration. In short, enumerate method takes two arguments viz. an iterable and a start value, defaults to 0 and adds a counter to each element of the iterable and returns an enumerator object which can then be converted to list of tuples or aam directly be iterated over( as in this case). For example : list(enumerate(a)) : [(0,2),(1,4),(2,6),(3,8)] In your case, list(enumerate(a, 2)): [(2,2),(3,4),(4,6),(5,8)], counter starts at 2 instead of 0.
22nd Jul 2019, 8:47 AM
Шащи Ранжан
Шащи Ранжан - avatar
+ 4
For the reason of correctness I would like to raise my hand here. Anil, if you look at the code as shown, you can see the variables p and n. In case of using enumerate the first variable ‘p’ will get the counter or index. The second variable ‘n’ will get the value from the iterable ‘a’. Your description is the other way around: “ … Here we are using p and n, p is to store the actual value of the number in the list and n for iterating over the list “ for p, n in enumerate(a): a = [2, 4, 6, 8] sum = 0 for p, n in enumerate(a): sum += n print(f'index: {p}, value: {n}') print(sum) #Output: index: 0, value: 2 index: 1, value: 4 index: 2, value: 6 index: 3, value: 8 20
23rd Jul 2019, 4:56 PM
Lothar
Lothar - avatar