Python Forum
I can't understand this problem
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
I can't understand this problem
#1
So sorry, I'm a beginner with programming and I face something in lists confused me.
I know that when I concatenate 2 lists with (+) I create a new list.
I'm using Python 2.7

list1 = [1,2,3]
list1 = list1 + [4,5]
print list1
here I get list1 = [1,2,3,4,5]
but where I put this in a procedure I don't get the same result
list2 = [3,4,5]
def mylist(my_list):
    my_list = my_list + [6,7]
mylist(list2)
print list2
here I get list2 = [3,4,5]
Why that?
another question why I get "none" message when I try to print
print mylist(list2)
. What is the difference between calling the procedure then print it
mylist(list2)
print list2

and print directly the procedure
print mylist(list2)
Reply
#2
The first answer is a little tricky. Lists are mutable types. One consequence of this is that if you modify a list you passed to a function, the list outside will be modified also. But you didn't modify the list. You reassigned the variable name of the parameter to a new list. The variable name exists only inside the function, and has no affect outside of it.

Note that my_list + [4, 5] is almost the same as my_list.extend([4, 5]). The difference is that the addition creates a new list you can reassign, while the second modifies the original list in place.

The second question is easier. When you print a function call, you are printing the return value of that function. Since you didn't specify a return value with a return statement, you get the default return value of None. If you had ended your function with return my_list, printing the function call would have printed the modified list.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#3
(Dec-23-2018, 08:36 PM)ichabod801 Wrote: The first answer is a little tricky. Lists are mutable types. One consequence of this is that if you modify a list you passed to a function, the list outside will be modified also. But you didn't modify the list. You reassigned the variable name of the parameter to a new list. The variable name exists only inside the function, and has no affect outside of it.

Note that my_list + [4, 5] is almost the same as my_list.extend([4, 5]). The difference is that the addition creates a new list you can reassign, while the second modifies the original list in place.

The second question is easier. When you print a function call, you are printing the return value of that function. Since you didn't specify a return value with a return statement, you get the default return value of None. If you had ended your function with return my_list, printing the function call would have printed the modified list.

Thank you very much for your answer
for the second answer, I understand what is wrong with my code
but the first answer, I understand - if I'm right - that when I modify the list inside the function by reassigning it to another variable. I will receive the original list.
OK what about this code
list2 = [1,2,3,4,5]
def fun_2(mylist):
    mylist.append(6)
    mylist.append(7)
print list2   # here the result is [1,2,3,4,5]
fun_2(list2)
print list2    # here why the result is [1,2,3,4,5,6]
it is the same but I use here Append which mutate the original list but also inside the function, So why here I see the mutated list not the original

SO SORRY IF THE QUESTIONS SEEM STUPID. Shy
Reply
#4
Lists are also "reference pointers." A reference pointer stores the memory reference for the data instead of containing copies of the data. It's a more advanced concept that's much more prevalent in other programming languages (C, Go, Java, etc.). Basically, when you pass the list into the function, the function doesn't make a local copy of the data. Rather, it looks directly at the memory space where that list is stored. This means that any alteration made within the function is made directly to the original list.

This gets really interesting when you do something like this:

in_list = []
out_list = []

for x in range(4):
    in_list.append(x)
    out_list.append(in_list)

print(out_list)
It will print:
Output:
[[0,1,2,3],[0,1,2,3],[0,1,2,3],[0,1,2,3]]
The loop appends a copy of the in_list to the out_list each time. It also appends new data to the in_list. When we print the out_list, we may expect four copies of the in_list at different stages of the iteration: [[0],[0,1],[0,1,2],[0,1,2,3]]. Instead, we get four copies of the in_list with all the numbers in each copy. This is because the memory reference for the in_list has been appended four times and that memory reference can only contain the most recent data stored there.
Reply
#5
(Dec-23-2018, 09:17 PM)muhammedkhairy Wrote: it is the same but I use here Append which mutate the original list but also inside the function, So why here I see the mutated list not the original

It's not the same. When you append, you modify the original list. As stulis notes, mylist is pointing to the location of list2. In your original code, you created a new list by adding two lists together. This does not modify the original list. In that code, my_list was pointing to the location of list2, but then you reassign it (with the = assignment operator) to the location of the new list you just created by adding.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#6
Now, I understand that.
Thank you all very much
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Couldn't really understand the problem Batselot 1 2,282 Dec-13-2018, 07:59 AM
Last Post: Gribouillis

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020