extension to list extend

Discussion in 'Python' started by James Stroud, Oct 16, 2007.

  1. James Stroud

    James Stroud Guest

    Found that this would be handy today:

    alist = [1, 2, 3]
    alist.extend(['a', 'b', 'c'], 1)
    alist == [1, 'a', 'b', 'c', 2, 3] # True

    James

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

    http://www.jamesstroud.com
     
    James Stroud, Oct 16, 2007
    #1
    1. Advertising

  2. James Stroud

    Peter Otten Guest

    James Stroud wrote:

    > Found that this would be handy today:
    >
    > alist = [1, 2, 3]
    > alist.extend(['a', 'b', 'c'], 1)
    > alist == [1, 'a', 'b', 'c', 2, 3] # True


    A better name for that would be insert(), but that is already used for
    single-item insertion. For the time being you can do

    >>> items = [1, 2, 3]
    >>> items[1:1] = "abc"
    >>> items

    [1, 'a', 'b', 'c', 2, 3]

    Peter
     
    Peter Otten, Oct 16, 2007
    #2
    1. Advertising

  3. James Stroud

    James Stroud Guest

    Peter Otten wrote:
    > James Stroud wrote:
    >
    >> Found that this would be handy today:
    >>
    >> alist = [1, 2, 3]
    >> alist.extend(['a', 'b', 'c'], 1)
    >> alist == [1, 'a', 'b', 'c', 2, 3] # True

    >
    > A better name for that would be insert(), but that is already used for
    > single-item insertion. For the time being you can do
    >
    >>>> items = [1, 2, 3]
    >>>> items[1:1] = "abc"
    >>>> items

    > [1, 'a', 'b', 'c', 2, 3]


    Thanks! It looks much nicer too.

    James

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

    http://www.jamesstroud.com
     
    James Stroud, Oct 16, 2007
    #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. sdhyok
    Replies:
    2
    Views:
    303
    sdhyok
    Aug 25, 2003
  2. Chris Rebert

    Re: faster than list.extend()

    Chris Rebert, Nov 16, 2009, in forum: Python
    Replies:
    2
    Views:
    306
    Jason Sewall
    Nov 17, 2009
  3. Gabriel Genellina

    Re: faster than list.extend()

    Gabriel Genellina, Nov 17, 2009, in forum: Python
    Replies:
    1
    Views:
    342
    Peter Otten
    Nov 17, 2009
  4. Dan Brown

    list.extend([]) Question

    Dan Brown, Jan 30, 2010, in forum: Python
    Replies:
    8
    Views:
    394
    Jack Diederich
    Feb 5, 2010
  5. kj
    Replies:
    3
    Views:
    159
Loading...

Share This Page