Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #65386

Re: [newbie] copying identical list for a function argument

Date 2014-02-03 15:51 -0600
From Tim Chase <python.list@tim.thechases.com>
Subject Re: [newbie] copying identical list for a function argument
References <df3d41dd-222d-4a05-b9de-377d67b77bda@googlegroups.com>
Newsgroups comp.lang.python
Message-ID <mailman.6370.1391468667.18130.python-list@python.org> (permalink)

Show all headers | View raw


On 2014-02-03 13:36, 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?
> 
> 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

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

[newbie] copying identical list for a function argument Jean Dupont <jeandupont314@gmail.com> - 2014-02-03 13:36 -0800
  Re: [newbie] copying identical list for a function argument Gary Herron <gary.herron@islandtraining.com> - 2014-02-03 14:06 -0800
  Re: [newbie] copying identical list for a function argument Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2014-02-03 22:19 +0000
    Re: [newbie] copying identical list for a function argument Jean Dupont <jeandupont115@gmail.com> - 2014-02-04 01:14 -0800
  Re: [newbie] copying identical list for a function argument Tim Chase <python.list@tim.thechases.com> - 2014-02-03 15:51 -0600
  Re: [newbie] copying identical list for a function argument Rustom Mody <rustompmody@gmail.com> - 2014-02-03 19:09 -0800

csiph-web