FAQ: Learn Python - Lists and Functions - Iterating over a list in a function

This community-built FAQ covers the “Iterating over a list in a function” exercise in Codecademy’s lessons on Python.

FAQs for the Codecademy Python exercise Iterating over a list in a function:

Join the Discussion. We Want to Hear From You!

Have a new question or can answer someone else’s? Reply (reply) to an existing thread!

Agree with a comment or answer? Like (like) to up-vote the contribution!

Need broader help or resources about Python in general? Go here!

Want to take the conversation in a totally different direction? Join our wider discussions.

Learn more about how to use this guide.

Found a bug? Report it!

Have a question about your account, billing, Pro, or Pro Intensive? Reach out to our support team!

None of the above? Find out where to ask other questions here!

Other FAQs

The following are links to additional questions that our community has asked about this exercise:

  • This list will contain other frequently asked questions that aren’t quite as popular as the ones above.
  • Currently there have not been enough questions asked and answered about this exercise to populate this FAQ section.
  • This FAQ is built and maintained by you, the Codecademy community – help yourself and other learners like you by contributing!

Not seeing your question? It may still have been asked before – try (search) in the top-right of this page. Still can’t find it? Ask it below by hitting the reply button below this post (reply).

2 posts were split to a new topic: Iterating Through Lists

if i understand this example correctly, the code is supposed to take each number from the list and add it to one another.
when i run the code as:

n = [3, 5, 7]

def total(numbers):
  result=0
  for stuff in numbers:
    result+=stuff
  return result
print total(n)

i get a result of 15, which the program accepts

But if i run the code:

n = [3, 5, 7]

def total(numbers):
  result=0
  for stuff in range(len(numbers)):
    result+=stuff
  return result
print total(n)

i get a result of 3.
Can someone please help explain to me why. Thank you

the difference is in the for loop:

for stuff in numbers:
for stuff in range(len(numbers)):

in the first example, you get the values from the list, in second example (using range) you get the indices. So then you would need to use the index to get the value from the list before adding it to result

2 Likes

Example 2: body of for loop should be result += numbers[stuff] for the reason stetim94 explained above.

3 Likes

alright cool. so with the range(len(numbers)) alternative, what purpose does the len() portion serve? if range() runs through each of the indices and can call the indices individually as well as where to stop, why the len()?

range produces a list with numbers, using len we can generate the right indices values (using range) for the numbers list. I assumed you knew, so i cut some corners.

2 Likes

I’m trying dude. Some concepts make more sense than others, like the range(len)) conjunctions in for loops

this should help:

n = [3, 5, 7]

print range(len(n))

for index in range(len(n)):
   print index, n[index]

range produce a list with indices, so we don’t have to manually type all the indices:

n[0]
n[1]
n[2]
3 Likes

thank you. so does len() specify how far in the list to go? for example, does ‘len(n)’ tell the code to produce the entire list? and can len() tell the code to run not the entire ‘n’ list?

len() will give you the length of the list, you could also do what i just did:

n = [3, 5, 7]

print len(n)

to get more information about something you don’t know, or check the python documentation:

https://docs.python.org/2/library/functions.html#len
https://docs.python.org/2/library/functions.html#range

2 Likes

alright great. thank you for your time with my trivial questions

n = [3, 5, 7]

def total(number):
result = 0
for number in result:
return result
is this right
i don not get it

have you tried calling the function to see if you get the expected output?

1 Like

oh,i forgot thanks a lot

Teaching yourself to debug your own code is something very important. So teaching yourself to ask questions like: How can i verify the code works correctly? How could i test it?

1 Like

I wrote the following code for this exercise:

n = [3, 5, 7]

def total(numbers):

  result = 0

  for i in range(0,len(numbers)):

    result += numbers[i]

    return result

But it gave me an error and marked it as incorrect. However, when I checked its solution it was same as I wrote it. Can anyone help me know where the problem is?
Thanks

Why you use range()? You are only interested in the values, not the indexes.

Your code and the solution are not the same. Your function reaches the return keyword in the first iteration of the loop, given us the total/sum of the first value

The solution adds all the values together, then after the loop returns the sum/total.

It was written that I could use any method of iteration.
This code is giving the required result. Is something wrong with it ?