FAQ: Learn Python - Lists and Functions - Modifying an element of a list in a function

This community-built FAQ covers the “Modifying an element of a list in a function” exercise in Codecademy’s lessons on Python.

FAQs for the Codecademy Python exercise Modifying an element of 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).

Hi.

Could someone explain to me how print numbers print out 2,2,3,4 when there is no return in the function? In my mind it should be return n[0] = n[0] * 2

def double_first(n):
  n[0] = n[0] * 2

numbers = [1, 2, 3, 4]
double_first(numbers)
print numbers

The function itself has nothing to do with this, take a look at this code:

a = 8
b = a
# here b has the same value as variable a
# but b is not a, it is just a copy
b = b * 2
print(a, b)

a = [1, 2, 3, 4]
b = a
# here b is the same object as a
# any modification of the value of b will result in modification of a
b[0] = b[0] * 2
print(a, b)

Evaluation of this code will result in:

8 16
[2, 2, 3, 4] [2, 2, 3, 4]

Interesting, right? Value of list a has changed, without any direct modification on our side!

In Python some objects are immutable (like numerical values) and some objects are mutable (like lists). It might be a bit confusing at start but you will get used to it :slight_smile: I have found a good decription of this mechanism that will shed some more light -> https://nedbatchelder.com/text/names1.html, and it is definitely better written than I would be able to.

Hi!

in the explanationcode the function doesn’t return anything. the function is called and after that the list is printed and it works:

def double_first(n):
  n[0] = n[0] * 2

numbers = [1, 2, 3, 4]
double_first(numbers)
print numbers

in the exercise, it’s needed that the function returns the list:

def list_function(x):
  x[1] = x[1] + 3
return x

n = [3, 5, 7]
print list_function(n)

can anyone explain to me what the difference is?

greetings,
Marcus

Hey @marcusde36ste, I formatted your code. Go ahead and check out this post as a reference on how to do this.

Try checking out this topic below, it might answer your question:

https://www.codecademy.com/forum_questions/518ffbfeb3f05c44fe001395

1 Like

Hi Steven

print 'thanks a lot! the difference between return and print is a lot clearer now :)'

1 Like