copy list, which way is best? /style

Discussion in 'Python' started by Andreas Kuntzagk, Jul 9, 2003.

  1. Hi,

    There are three ways to (shallow)copy a list l I'm aware of:

    >>> l2=list(l)
    >>> l2=l[:]
    >>> l2.copy.copy(l)


    Are there any differences? Are there more (reasonable) ways?
    I think the first is the most pythonic, second looks more like this other
    p-language and third requires an import, so I would prefer the first.
    Do you agree?

    Andreas
     
    Andreas Kuntzagk, Jul 9, 2003
    #1
    1. Advertising

  2. Andreas Kuntzagk wrote:

    > Hi,
    >
    > There are three ways to (shallow)copy a list l I'm aware of:
    >
    >>>> l2=list(l)
    >>>> l2=l[:]
    >>>> l2.copy.copy(l)

    >
    > Are there any differences? Are there more (reasonable) ways?
    > I think the first is the most pythonic, second looks more like this other
    > p-language and third requires an import, so I would prefer the first.
    > Do you agree?
    >
    > Andreas


    The way I'd do it is

    from copy import copy
    l2 = copy(l1)

    or

    from copy import deepcopy
    l2 = deepcopy(l1)

    I don't know what the difference is, if any, but I think this way is more
    readable.

    HTH,
    Andrew Wilkinson

    --
    Study + Hard Work + Loud Profanity = Good Code
     
    Andrew Wilkinson, Jul 9, 2003
    #2
    1. Advertising

  3. Andreas Kuntzagk

    Duncan Booth Guest

    "Andreas Kuntzagk" <> wrote in
    news:begt35$4sd5m$:

    > There are three ways to (shallow)copy a list l I'm aware of:
    >
    >>>> l2=list(l)
    >>>> l2=l[:]
    >>>> l2.copy.copy(l)

    >
    > Are there any differences? Are there more (reasonable) ways?
    > I think the first is the most pythonic, second looks more like this other
    > p-language and third requires an import, so I would prefer the first.
    > Do you agree?


    They all do slightly different things.
    I think this is a fairly accurate description of what each of these does:

    >>> l2 = list(l)

    This will copy any iterable object and will produce a new, distinct list as
    a result.

    >>> l2=l[:]

    This will copy a sequence, and will return an object of the same type as
    the original. If the original is immutable, then it may simply return the
    original object and not bother with making a copy.

    >>> l2=copy.copy(l)

    This will copy any object whether or not it is a sequence, but it may still
    return the original object for immutables.

    >>> l2=copy.deepcopy(l)

    This will make a deep copy of an object. It only returns the original
    object for immutables if any objects they contain are also immutable
    (including their contents).

    --
    Duncan Booth
    int month(char *p){return(124864/((p[0]+p[1]-p[2]&0x1f)+1)%12)["\5\x8\3"
    "\6\7\xb\1\x9\xa\2\0\4"];} // Who said my code was obscure?
     
    Duncan Booth, Jul 9, 2003
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Alex
    Replies:
    2
    Views:
    1,272
  2. Frank Millman

    Best way to create a copy of a list

    Frank Millman, Apr 4, 2006, in forum: Python
    Replies:
    7
    Views:
    287
    Frank Millman
    Apr 5, 2006
  3. Replies:
    26
    Views:
    2,158
    Roland Pibinger
    Sep 1, 2006
  4. Ken Varn
    Replies:
    0
    Views:
    504
    Ken Varn
    Apr 26, 2004
  5. Eddy Xu
    Replies:
    5
    Views:
    137
    Eddy Xu
    Apr 11, 2008
Loading...

Share This Page