Re: Python: Question About Extend And Append Built-in Functions

  • From: Alex Hall <mehgcap@xxxxxxxxx>
  • To: programmingblind@xxxxxxxxxxxxx
  • Date: Wed, 8 Dec 2010 10:49:53 -0500

The append method will ad the object as its own element, so appending
a list to a list will give you a 2d list (or at least will give you a
list as one element of your parent list).
Extend, on the other hand, will add the elements of the list to the
list being extended. This means that, insteading of the entire list
being added as one element, all elements are appended one at a time.
For example:

l1=[1,2]
l2=[3,4]
l1.append(l2)
will give us a list with elements 1, 2, and [3,4]. This means that
l1[2] will be the entire list [3,4], not just 3, and l1[3] will throw
an exception.

However, l1.extend(l2) will give us a list 1, 2, 3, 4, so l1[2]=3 and
l1[3]=4 and is now valid. I hope this makes sense.

On 12/8/10, Homme, James <james.homme@xxxxxxxxxxxx> wrote:
> Hi,
> One of the most frustrating things in life is to try to read programming
> documentation written by programmers. It's not clear at all, so here goes my
> questioning.
>
> Are they trying to say that the difference between append and extend is
> basically that you can only append one of something to the end of the list
> and that when you use extend that you can append multiple things? And a
> related question is: when you use extend, are you allowed to put any kind of
> thing you want onto the end of a list? For example, if you have a list of
> strings, can you use extend to put a string and a list of strings onto the
> end of your list? I don't know why you'd do that, but I'm just asking if you
> can.
>
>                 Thanks.
>
> Jim
> Jim Homme,
> Usability Services,
> Phone: 412-544-1810. Skype: jim.homme
> Internal recipients,  Read my accessibility
> blog<http://mysites.highmark.com/personal/lidikki/Blog/default.aspx>.
> Discuss accessibility
> here<http://collaborate.highmark.com/COP/technical/accessibility/default.aspx>.
> Accessibility Wiki: Breaking news and accessibility
> advice<http://collaborate.highmark.com/COP/technical/accessibility/Accessibility%20Wiki/Forms/AllPages.aspx>
>
>
> ________________________________
> This e-mail and any attachments to it are confidential and are intended
> solely for use of the individual or entity to whom they are addressed. If
> you have received this e-mail in error, please notify the sender immediately
> and then delete it. If you are not the intended recipient, you must not
> keep, use, disclose, copy or distribute this e-mail without the author's
> prior permission. The views expressed in this e-mail message do not
> necessarily represent the views of Highmark Inc., its subsidiaries, or
> affiliates.
>


-- 
Have a great day,
Alex (msg sent from GMail website)
mehgcap@xxxxxxxxx; http://www.facebook.com/mehgcap
__________
View the list's information and change your settings at 
//www.freelists.org/list/programmingblind

Other related posts: