How to pass a list by reference?

Lists are already passed by reference, in that all Python names are references, and list objects are mutable. Use slice assignment instead of normal assignment.

def add(L1, L2, L3):
    L3[:] = L1 + L2

However, this isn’t a good way to write a function. You should simply return the combined list.

def add(L1, L2):
    return L1 + L2

L3 = add(L1, L2)

Leave a Comment