assignment with [:]

Discussion in 'Python' started by Ben Bush, Dec 28, 2008.

  1. Ben Bush

    Ben Bush Guest

    Hi,

    I saw this line of code on a recent post:

    a1[:] = [x*3 for x in a1]

    Could somebody tells me what the [:] means? I can't find it anywhere.
    See context below if needed:

    On Dec 26, 4:46 pm, Tim Chase <> wrote:
    > > What does *not* work is
    > > 3 * [0,1,2]
    > > As you know, this gives
    > > [0,1,2,0,1,2,0,1,2]
    > > What I am hoping for is
    > > [0,3,6]
    > > I see that I can use
    > > numpy.multiply(3,range(3))
    > > but this seems overkill to me. Can you tell I am coming to Python from
    > > Matlab?

    >
    > The common way to do this is just
    >
    > a1 = [0,1,2]
    > a2 = [x * 3 for x in a1]
    >
    > or, if you need a1 to be done in place:
    >
    > a1[:] = [x*3 for x in a1]
    >
    > -tkc
     
    Ben Bush, Dec 28, 2008
    #1
    1. Advertising

  2. Ben Bush

    Robert Kern Guest

    Ben Bush wrote:
    > Hi,
    >
    > I saw this line of code on a recent post:
    >
    > a1[:] = [x*3 for x in a1]
    >
    > Could somebody tells me what the [:] means? I can't find it anywhere.


    It's a slice assignment. When both the start and stop arguments are omitted, it
    refers to the entire sequence. In this case, it means to replace the entire
    contents of the list a1 with the value on the right-hand-side.

    "a[2:4] = ..." would replace the 3rd and 4th items in the list with the sequence
    on the right-hand-side, for instance.

    --
    Robert Kern

    "I have come to believe that the whole world is an enigma, a harmless enigma
    that is made terrible by our own mad attempt to interpret it as though it had
    an underlying truth."
    -- Umberto Eco
     
    Robert Kern, Dec 28, 2008
    #2
    1. Advertising

  3. Ben Bush

    James Stroud Guest

    Ben Bush wrote:
    > On Dec 26, 4:46 pm, Tim Chase <> wrote:
    >>> What does *not* work is
    >>> 3 * [0,1,2]
    >>> As you know, this gives
    >>> [0,1,2,0,1,2,0,1,2]
    >>> What I am hoping for is
    >>> [0,3,6]
    >>> I see that I can use
    >>> numpy.multiply(3,range(3))
    >>> but this seems overkill to me. Can you tell I am coming to Python from
    >>> Matlab?

    >> The common way to do this is just
    >>
    >> a1 = [0,1,2]
    >> a2 = [x * 3 for x in a1]
    >>
    >> or, if you need a1 to be done in place:
    >>
    >> a1[:] = [x*3 for x in a1]


    There is some subtlety here. The latter says to empty the list assigned
    to the name "a1" and refill it with the products. Other references to
    the same list will now reflect this operation. This procedure is not
    equivalent to reassigning the name "a1". For example:

    py> a = [1, 2, 3]
    py> a1 = a
    py> a1[:] = [x*3 for x in a1]
    py> a1
    [3, 6, 9]
    py> a1
    [3, 6, 9]

    Whereas:

    py> a = [1, 2, 3]
    py> a1 = a
    py> a1
    [1, 2, 3]
    py> a1 = [x*3 for x in a1]
    py> a1
    [3, 6, 9]
    py> a
    [1, 2, 3]

    James

    --
    James Stroud
    UCLA-DOE Institute for Genomics and Proteomics
    Box 951570
    Los Angeles, CA 90095

    http://www.jamesstroud.com
     
    James Stroud, Dec 28, 2008
    #3
  4. Ben Bush

    James Stroud Guest

    James Stroud wrote:
    > py> a = [1, 2, 3]
    > py> a1 = a
    > py> a1[:] = [x*3 for x in a1]
    > py> a1
    > [3, 6, 9]
    > py> a1
    > [3, 6, 9]



    This should have been:

    py> a = [1, 2, 3]
    py> a1 = a
    py> a1[:] = [x*3 for x in a1]
    py> a
    [3, 6, 9]
    py> a1
    [3, 6, 9]


    James


    --
    James Stroud
    UCLA-DOE Institute for Genomics and Proteomics
    Box 951570
    Los Angeles, CA 90095

    http://www.jamesstroud.com
     
    James Stroud, Dec 28, 2008
    #4
    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. itsme
    Replies:
    1
    Views:
    1,633
    Ralf Hildebrandt
    Jul 23, 2003
  2. Anand P Paralkar
    Replies:
    2
    Views:
    9,396
    Srinivasan Venkataramanan
    Aug 4, 2003
  3. John Potter

    Question: inout signal assignment

    John Potter, Sep 6, 2003, in forum: VHDL
    Replies:
    1
    Views:
    1,728
    Renaud Pacalet
    Sep 8, 2003
  4. nagy
    Replies:
    36
    Views:
    1,011
    Terry Reedy
    Jul 20, 2006
  5. Chris
    Replies:
    34
    Views:
    1,523
Loading...

Share This Page