Help with understanding difference between these codes

  • Thread starter Thread starter Kolika28
  • Start date Start date
  • Tags Tags
    Difference
Click For Summary
The discussion focuses on two pieces of Python code that illustrate different behaviors regarding list manipulation. The first code snippet defines a function that doubles the elements of a list and returns a new list, leaving the original list unchanged. It emphasizes the importance of printing the result to see the output. The second code snippet modifies the original list in place by incrementing its first element, highlighting that the function does not need to return the modified list since the change is directly applied to the original. The conversation underscores a best practice in Python: functions that mutate objects in place should ideally not return anything, as this clarifies their intent. The final point made is that if the result of the first function is assigned back to the original variable, the original list would then reflect the changes.
Kolika28
Messages
146
Reaction score
28
TL;DR
I don't understand why the first code won't change the list x, while the second code does change the list. Could someone explain?
Python:
#Code nr.1
x=[1,2]
def double(l):
    return [a*2 for a in l]
double(x)
print(x)

#Code nr.2
def f(x):
    x[0]=x[0]+1
    return x
x=[3]
f(x)
print(x)
 
Technology news on Phys.org
The first code returns a newly constructed list whose elements are the elements of the original list multiplied by two. That doesn't change anything about the original list. Try having print(double(x)) instead of just double(x) and see what happens.

The second code doesn't construct a new list, it sets the first element of the list to its previous value plus 1.
 
  • Like
Likes Kolika28
Note also that in the second code, f(x) returns the list x, but that return value isn't needed since you already have a variable pointing to the list x. It's a good general practice in Python that a function that mutates an object in place, instead of constructing a new object, should not return anything. That makes it clear that the function is not constructing anything new.
 
  • Like
Likes Kolika28
[x for x in mylist] is a copy of mylist. Your first function uses this to create a modified copy, which it returns and you don't store or display. Your second function edits the list in place. You return the edited list and forget that too, but because you've edited it in place the original list is modified.

Edit: beaten to it by Peter, I see.
 
  • Like
Likes Kolika28
Thank you so much, both of you! I understand now ! :)
 
If you were to say:

x=double(x)

then x would be changed.
 
  • Like
Likes Kolika28
Learn If you want to write code for Python Machine learning, AI Statistics/data analysis Scientific research Web application servers Some microcontrollers JavaScript/Node JS/TypeScript Web sites Web application servers C# Games (Unity) Consumer applications (Windows) Business applications C++ Games (Unreal Engine) Operating systems, device drivers Microcontrollers/embedded systems Consumer applications (Linux) Some more tips: Do not learn C++ (or any other dialect of C) as a...

Similar threads

  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 18 ·
Replies
18
Views
1K
Replies
55
Views
6K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 16 ·
Replies
16
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 4 ·
Replies
4
Views
5K
  • · Replies 10 ·
Replies
10
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K