Help with understanding difference between these codes

  • Thread starter Thread starter Kolika28
  • Start date Start date
  • Tags Tags
    Difference
Click For Summary

Discussion Overview

The discussion focuses on understanding the differences between two Python code snippets, specifically regarding list manipulation and function behavior. The scope includes conceptual clarification and technical explanation of how functions can modify lists in place versus returning new lists.

Discussion Character

  • Conceptual clarification
  • Technical explanation

Main Points Raised

  • Some participants explain that the first code snippet creates a new list with doubled values, leaving the original list unchanged.
  • Others note that the second code snippet modifies the original list in place by incrementing its first element.
  • It is mentioned that returning the modified list in the second code is unnecessary since the original list is already being altered.
  • One participant points out that using a list comprehension creates a copy of the list, which is relevant to the first function's behavior.
  • A later reply suggests that if the result of the first function were assigned back to x, then x would reflect the changes.

Areas of Agreement / Disagreement

Participants generally agree on the differences in behavior between the two code snippets, but there is no explicit consensus on best practices regarding function returns in Python.

Contextual Notes

There are assumptions about the reader's familiarity with Python functions and list behavior, which may not be universally applicable. The discussion does not resolve whether returning modified lists is a best practice.

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   Reactions: 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   Reactions: 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   Reactions: 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   Reactions: Kolika28

Similar threads

  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 18 ·
Replies
18
Views
2K
Replies
55
Views
7K
  • · 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
6K
  • · Replies 10 ·
Replies
10
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K