[newbie] copying identical list for a function argument

Steven D'Aprano steve+comp.lang.python at pearwood.info
Mon Feb 3 23:19:39 CET 2014

On Mon, 03 Feb 2014 13:36:24 -0800, Jean Dupont wrote:

> I have a list like this:
> [1,2,3]
> The argument of my function should be a repeated version e.g.
> [1,2,3],[1,2,3],[1,2,3],[1,2,3] (could be a different number of times
> repeated also)
> what is the prefered method to realize this in Python?

I don't really understand your question. It could mean any of various 
things, so I'm going to try to guess what you mean. If my guesses are 
wrong, please ask again, giving more detail, and possibly an example of 
what you want to do and the result you expect.

I think you mean that you have some function that needs to take (say) 
five arguments, and you want to avoid writing:

result = function([1, 2, 3], [1, 2, 3], [1, 2, 3], [1, 23], [1, 2, 3])

because it's too easy to make a mistake (as I did, deliberately, above -- 
can you see it?).

If my guess is correct, try this:

mylist = [1, 2, 3]
result = function(mylist, mylist, mylist, mylist, mylist)

That's perfectly reasonable for two or three arguments, but not so much 
for five. Instead, here's a trick: first we make five identical 
references to the same list:

[mylist]*5  # same as [mylist, mylist, mylist, mylist, mylist]

then expand them as arguments to the function:

mylist = [1, 2, 3]
list_of_lists = [mylist]*5
result = function(*list_of_lists)

(The * operator means multiplication when used between two arguments, and 
inside a function call a leading * also does argument expansion.)

But wait... there's something slightly weird here. Even though there are 
five distinct references to mylist, they're all the same list! Change 
one, change all. This may be what you want, or it may be a problem. Hard 
to tell from your question.

Think about references as being a little bit like names. A *single* 
person could be known as "son", "Dad", "Mr Obama", "Barack", "Mr 
President", "POTUS", and more. In this case, we have a single list, [1, 
2, 3], which is known by six references: the name "mylist", and five 
additional references list_of_lists index 0, list_of_lists index 1, and 
so on up to list_of_lists index 4.

We can prove that they all refer to the same list by running a bit of 
code in the interactive interpreter:

py> mylist = [1, 2, 3]
py> list_of_lists = [mylist]*5
py> list_of_lists
[[1, 2, 3], [1, 2, 3], [1, 2, 3], [1, 2, 3], [1, 2, 3]]
py> mylist.append(99)
py> list_of_lists
[[1, 2, 3, 99], [1, 2, 3, 99], [1, 2, 3, 99], [1, 2, 3, 99], [1, 2, 3, 

So rather than having five references to the same, identical, list, you 
might want five *copies*. You can copy a list using slicing:

mylist = [1, 2, 3]
copy = mylist[:]

Instead of using list multiplication to repeat five identical lists, we 
make five copies using a list comprehension:

list_of_lists = [mylist[:] for i in range(5)]

then expand it in the function call as before:

result = function(*list_of_lists)

Hope this helps,


More information about the Python-list mailing list