Python Forum
Passing a list by reference - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Passing a list by reference (/thread-11254.html)



Passing a list by reference - virgik89 - Jun-30-2018

Hi all, I'm trying to do a sort function and I'm having some trouble. The function is actually not sorting, it is something with passing the arguments that I don't understand. I know that the lists are passed by reference therefore this example should work, but it doesn't.
p/s please don't tell my about build-in function .sort()

import sys
from random import randint


def switch (a,b):
    temp = a
    a = b
    b = temp

def sortList(data):
    for x in range (len(data)):
        for y in range (len(data)):
            if data[x] > data[y]:
                switch(data[x],data[y])

L = []
data = int(input("how many numbers do you want?: "))
for i in range(data):
    L.append(randint(1,1000000))
print('unsorted',L)
sortList(L)
print('sorted',L)
===================output==================================================

how many numbers do you want?: 10
unsorted [365283, 240490, 441964, 411424, 78927, 737039, 305545, 808196, 595894, 857359]
sorted [365283, 240490, 441964, 411424, 78927, 737039, 305545, 808196, 595894, 857359]


RE: Passing a list by reference - gontajones - Jun-30-2018

Your function switch() must return the new values:

def switch(a, b):
    return b, a

a = 1
b = 2
a,b = switch(a,b)
Or, more directly...

def sortList(data):
    for x in range(len(data)):
        for y in range(len(data)):
            if data[x] > data[y]:
                data[x], data[y] = data[y], data[x]



RE: Passing a list by reference - ichabod801 - Jun-30-2018

You're not passing lists to switch, you are passing the values in the list. So the actual exchange isn't involving the lists.

Is this homework? Because this is just a really bad idea all around. But assuming it's homework, have you covered tuple assignment?

data[x], data[y] = data[y], data[x]
That will handle what the swap function does. Otherwise you will need to either do the swap stuff in the sortList function, or pass three things to the swap function: The whole list and the two indexes.

Note that your loop is going to mess it all up. You are looping through all of the indexes. So say you start the loop for x == 5. The first y value will be 0, and you will be comparing data[5] to data[0], which is way before data[5]. You need to look into sorting algorithms. This kind of looks like bubble sort, but there you just check adjacent indexes, and you check them until no further swaps are needed.


RE: Passing a list by reference - virgik89 - Jun-30-2018

(Jun-30-2018, 03:23 AM)ichabod801 Wrote: 1
data[x], data[y] = data[y], data[x]
interesting :)

No this is not a homework assignment. Yes it is a bubble sort.
I'm learning python by myself and perhaps I tried to bring some C++ in it:) Anyway, I see where the problem is: switch(I should've call it swap) function doesn't pass by reference as I understand.

Thanks @ichabod801 and @gontajones.


RE: Passing a list by reference - volcano63 - Jun-30-2018

(Jun-30-2018, 03:54 AM)virgik89 Wrote: Anyway, I see where the problem is: switch(I should've call it swap) function doesn't pass by reference as I understand.

No, they are passed as references - only as constant references. A function cannot change a reference to its argument - but it can change the referenced object content.

Thus, immutable objects cannot be changed within a function - but elements of a mutable object may be.

This simple function will change the list in the outer scope even if you don't return the list reference - but this will cause a side effect, and we don't want that, do we ?!
def foo(list_, value):
    list_.append(value)



RE: Passing a list by reference - ljmetzger - Jun-30-2018

Traditional language call by value and call by reference take a backseat in Python to call by object reference.

In addition to @volcano63's excellent comments, see the following which discusses all three concepts in Python: https://stackoverflow.com/questions/13299427/python-functions-call-by-reference

Lewis


RE: Passing a list by reference - virgik89 - Jul-01-2018

Thanks everyone.
Here is a good example that talks about immutability which make sense now to me how those references work.