FAQ: Learn Python – Lists and Functions – Printing out a list item by item in a function

Community%20FAQs%20on%20Codecademy%20Python%20Exercises

This community-built FAQ covers the “Printing out a list item by item in a function” exercise in Codecademy’s lessons on Python.

FAQs for the Codecademy Python exercise Printing out a list item by item 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).

7 posts were split to a new topic: List index out of range

why this function has no return?

It does not need a return, since it has nothing to return. The purpose is in the name, print_list, which is the operation the function performs.

1 Like

so we should not necessarily return sth in defining a function?

Functions are a way of composing a piece of useful code that can be re-used over and over with variable parameters to produce an expected result given the inputs. Some functions do not act upon the data, but simply output it, or report it. Their job, as in the above case is over and done once the function has run. No states are changed. They have no effect on the data.

function add_ab(a, b) {
    return a + b;
}

The above function also does not affect any data, but simply reports a sum of two data points, which are returned so the sum can be carried forward in other program operations, as needed…

function add_list_item(lst, item) {
    lst.append(item);
}

The above does affect the data since the inputed list has had a new value added to it. It does not need to return the list since it already exists outside of the function. lst is merely a reference to that list which could be any list under any name.

In time, this will all make perfect sense once you have more experience and new concepts under your belt.

2 Likes

Using map() has an issue with len

Simple question: Could someone explain to me why we use ‘x’ in the function if the list is equal to ‘n’?

The variables we use in a loop are arbitrary.

for x in a_list:
    print x

x is a placeholder that takes one value at a time from the list, then that value is printed and the iterator moves to the next element.

Perhaps I don’t quite get your question. Could you provide your code, or a code example to go along with this question?

In the case of a function to perform this task,

def print_list(x):
    for i in range(0, len(x)):
        print x[i]

In the above, x is the local parameter that references the list we have passed to the function via the call argument.

Given that n is a list,

print_list(n)

will call the function on that list. x is the local reference to that same list. We use a local variable so that the function is not dedicated to one global object. In other words we can pass in ANY list. Were we to use n in the function, then it would only ever see the global list, n, even if we pass in a different reference.

1 Like

I think this lesson was made to piss me off. Im totally confused now because of the wordings it uses plus the expectations. The exercise was saying:

Define a function called print_list that has one argument called x .

Inside that function, print out each element one by one. Use the existing code as a scaffold.

Then call your function with the argument n .

Now I had to press for the solution because my code was wrong. But… the solution was giving me the below:

n = [3, 5, 7]

def print_list(x):
for i in range(0, len(x)):
print x[i]

print_list(n)

So, how on earth should I know that I can put def print_list(x): before for i in range(0, len(x)): ???

If someone can explain me this the way it make sense then I promise I quit from python. I don’t see any point of clicking on “Give me the solution” button since either the exercise or the high expectations confuses me. Of course I get it, it me who gets confused over on this but still I feel like there is a better explanation for things.

Learning to code can be frustrating. There’s a reason they’re called programming languages and when you and the computer can’t understand one another, it’s no different than having to communicate with another person when neither of you share a common tongue; in fact it’s probably worse. This also applies to the flurry of new terminology you have to pick up in order to grasp some of the problems.

It does get easier with time, but it’s the start when you have no basis and there’s no clear goal to even work towards that it’s easiest to get discouraged. If you can stick with it you’ll end up looking back at this and wondering how it ever even gave you pause. So keep at it eh.

In the way this is written we are looking to create a function called to print_list that takes one argument (x). In Python that would be written in the following way-

def print_list(x):

The next line asks us to work inside this function and use the function itself to print a list one-by-one.
In Python indentation defines the grouping of statements and for code to be considered inside the function (will only run when this function is called) it must be indented following the definition of the function.

A quick example would be the following function that takes a number, multiplies it by 2 and then prints the number.

def print_times_two(number):
    number = number * 2
    print number

For this function named print_times_two the lines ‘number = number * 2’ and ‘print number’ are inside the function. They will only ever be run when the function itself is called.

Since we are provided with code that prints out every item in a list one by one the requirement of this lesson is to move these lines inside the function. This means they have to follow immediately after the function is defined and they must be indented to match the same code block.
Note that indentation is also required in the for loop to group statements together to ensure only certain lines are repeated by this loop.

def print_list(x):
    for i in range(0, len(x)):
        print x[i]

It will take some getting used to but this kind of grouping will become clearer with more practice. Good luck.

1 Like

Thank you tgrtim for your time explaining me this although I knew all that. Indentations was never an issue before. My problem is a little bit different. Yes, I know Im frustrated over on this ( which will be much easier later on) but I do know that there is a better way to explain things. Most of the times these exercises expect us to know or to genuinely think of another solution which is a good point. But, most of the times we all forget what we learnt 10 lessons ago. You are right, 2 different person talking in 2 different languages is not great, they will never understand each other. My point is that for example " if a teacher can’t explain things to the students in a way that they would understand then im sure the teacher have to look for another solution." I am not trying to blame others, i’m only saying that there is a weird pattern since I started to code here on this site. Some topics description makes no sense and I won't be able to use my intuition to figure out the one thing that I never knew and/or never thought of. Also I think the "Hints" could really give some info away about the topic rather than the whole solution or stating the obvious. I can only speak for myself, I like to learn things in a way where I do have to use my brain with a little Hint time to time. Dont get me wrong, I do appreciate the opportunity to learn here and I hope that my feedback doesn’t come across offensively.

Take care

n = [3, 5, 7]

def print_list(x):
  for i in range(0, len(x)):
    print x[i]

print print_list(n)

So the above works as the solution for this, but I’m just wondering, is the below piece of code not a simpler way of achieving the same thing? Is there a possible issue with the code below or could there be an advantage with using the above?

n = [3, 5, 7]

def print_list(x):
  for i in x:
    print i

print print_list(n)

Also, both methods output the list, but also print out “None” at the end - why is this? Thanks in advance

using range is useful when you need to indexes/indices to update elements in the list, which is not the case here, so then its just easier to get elements directly

the function doesn’t return anything, yet you print the returned result, which will give None (the absence of a return value)

2 Likes

Got it, thanks for the quick reply!!