Append a List to a List in Python

March 19, 2008 at 4:47 pm 8 comments

(Note: Please see my latest posts at my new blog!)

An easy way to do this is with the extend function:

x = [1,2,3]

x.extend([4,5])

[1,2,3,4,5]

About these ads

Entry filed under: CodeSnippet, Python. Tags: , .

Simple Method to Calculate Median in Python How to Install tar.gz Files in Linux

8 Comments Add your own

  • 1. paddy3118  |  March 19, 2008 at 8:22 pm

    The addition operator works too:

    >>> [1,2] + [3,4]
    [1, 2, 3, 4]
    >>>

    – Paddy,

  • 2. gawbul  |  July 27, 2010 at 3:54 pm

    list.append(list) works too :)

  • 3. gawbul  |  July 27, 2010 at 3:56 pm

    well actually, it would be

    list1.append(list2)

    but I’m sure you get the picture :D

  • 4. gawbul  |  July 27, 2010 at 3:59 pm

    Actually, delete them last two! No it doesn’t!

    extend works best :)

  • 5. hgjhghj  |  August 28, 2011 at 3:28 am

    lol fail

  • 6. Babu  |  April 3, 2012 at 8:56 pm

    Let’s say I have a list.
    The elements of the list are also lists.

    I want to append items to the lists in the lists with something like…

    POSITION_VECTOR_LIST= [ X_LIST, Y_LIST, Z_LIST]

    X_LIST=[ x1, x2, x3, ...etc ]
    etc…for Y and Z

    POSITION_VECTOR_LIST1[ { [0], [1], [2] } ].append( { X, Y, Z } )

    Where I want X to append to the the X_LIST (the x list is the first element in the POSITION_VECTOR_LIST

    (this is not real code fyi, it’s just a pseudo code for something I want to do…)

  • 7. Babu  |  April 3, 2012 at 8:58 pm

    there’s a typo, in the second appearance of POSITION_VECTOR_LIST

    POSITION_VECTOR_LIST1[ { [0], [1], [2] } ].append( { X, Y, Z } )

    I didn’t mean to write …LIST1[…
    s’posed to be …LIST[…

  • 8. Ta  |  April 10, 2013 at 4:18 am

    I came to your “Append a List to a List in Python | Code Comments” page and noticed you could have a lot more traffic. I have found that the key to running a website is making sure the visitors you are getting are interested in your subject matter. There is a company that you can get traffic from and they let you try it for free. I managed to get over 300 targetted visitors to day to my website. Check it out here: http://voxseo.com/traffic/

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Trackback this post  |  Subscribe to the comments via RSS Feed



Follow

Get every new post delivered to your Inbox.

%d bloggers like this: