FAQ: Learn Python - Lists and Functions - List manipulation in functions

faq

#1

This community-built FAQ covers the “List manipulation in functions” exercise in Codecademy’s lessons on Python.

FAQs for the Codecademy Python exercise List manipulation in 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!


#2

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).


#3

why do I get “None” answer in response to my code as below:
def list_extender(lst):
result = lst.append(9)
return result


#4

.append() does not return anything, hence result is None.

 lst.append(9)
 return lst

#5

Why do we have to return the list if we are explicitly changing the list within the function?


#6
>>> def list_extender(lst):
    lst.append(9)
    return lst

>>> alist = [1, 3, 5, 7]
>>> blist = list_extender(alist)
>>> id(alist)
47863728
>>> id(blist)
47863728
>>> alist
[1, 3, 5, 7, 9]
>>> blist
[1, 3, 5, 7, 9]
>>> 

The above supports the assertion within your question. At this point we are left to dig for some reasoning, such as an advantage or benefit.

Is it a cloning method? Short answer, no.

>>> list_extender(blist)
[1, 3, 5, 7, 9, 9]
>>> alist
[1, 3, 5, 7, 9, 9]
>>> 

With a minor cfhange-up we can make it a cloner, with the mutation on the clone, and not the argument object.

>>> def list_extender(lst, x):
    temp = lst[:]
    temp.append(x)
    return temp

>>> alist = [1, 3, 5, 7]
>>> blist = list_extender(alist, 9)
>>> blist
[1, 3, 5, 7, 9]
>>> alist
[1, 3, 5, 7]
>>> 

With a little logic, we could leapfrog the two lists so they grow alternately, but symbiotically. That way the two lists would always be near copies of each other, and exist at the same time.

That just fell out of this question. Where would we need such logic? Who knows? But we now have it, just by exploring a question for which we still do not have an answer.


#7

Usually you would either change the list or return a new one. I’m not sure what the instructions here are asking for.
Unless is it the very purpose of a function to change the original, then the function should probably not modify the input.


#8

I have to agree with the above… We should not modify the inputs, but return a unique, unbound object to the caller to be bound on return. However, the author may not have that in mind when writing this instruction…

1. Define a function called list_extender that has one parameter lst .

Inside the function, append the number 9 to lst .

Then return the modified list.