[newbie] copying identical list for a function argument

J

Jean Dupont

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?

any help would be really appreciated

kind regards,
jean
 
T

Tim Chase

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?

any help would be really appreciated

It depends on whether you want the contained list to be the *same*
list or *copies* of that list. You can do either of the following:

lst = [1,2,3]
dupes1 = [lst[:] for _ in range(5)]
dupes2 = [lst for _ in range(5)]
dupes3 = [lst] * 5

The dupes2 and dupes3 should be the same (the latter is a simpler
syntax for it): each contains the same list multiple times. To see
this, do

lst.append(4)

and then inspect dupes1 compared to dupes2/dupes3.

-tkc
 
G

Gary Herron

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)

That's not very clear. You say "The" argument (singular; meaning 1) but
then show what seems to be four arguments. Can you show us the function
you mention.

A number of things come to mind:
[[1, 2, 3], [1, 2, 3], [1, 2, 3], [1, 2, 3]]

so fn(n*[[1,2,3]]) might do for a single parameter function
def fn(L):
...

On the other hand expanding a sequence into individual parameters:
fn(*(4*[[1,2,3]])) # note extra * preceding the arg
willl be a valid call for
def fn(a,b,c,d):
...

I'm sure other interpretations of your question are possible.

Gary Herron
 
S

Steven D'Aprano

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,
99]]


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,
 
R

Rustom Mody

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?

Probably not what you are asking... But maybe you want to
look at repeat (and other stuff) in itertools

[(0, 'hi'), (1, 'hi'), (2, 'hi'), (3, 'hi'), (4, 'hi'), (5, 'hi'), (6, 'hi'),
(7, 'hi'), (8, 'hi'), (9, 'hi')]


IOW repeat give you "indefinite-data-repeat" just as "while True:..."
gives "indefinite-code-repeat"
 
J

Jean Dupont

Op maandag 3 februari 2014 23:19:39 UTC+1 schreef Steven D'Aprano:
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,
99]]

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,
Yes it does, thanks a lot to you and all the others who responded, "the
missing link" which until now I wasn't aware of but which was essential
for the solution was the "*" in
result = function(*list_of_lists)

kind regards,
jean
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,042
Latest member
icassiem

Latest Threads

Top