Newbie Question, list = list + x VS append (x)

Andreas Jung andreas at andreas-jung.com
Fri Nov 29 02:21:12 EST 2002


Because "+" acts like appending every item of the right hand list
to the left hand list.

-aj

--On Freitag, 29. November 2002 07:14 +0000 Chuck <cdreward at riaa.com> wrote:

> Just a quick question from a Python newbie (I've only been reading the
> tutorial for about 10 minutes <g> )
>
> Could someone explain why these two bits of code act differently?
>
>
> def add2(n):
>     return [n+2]
>
> x = y = [1,2]
>
> print x # [1,2] - Ok
>
> x = x + add2(5)
>
> print x # [1,2,7] - why not [1,2,[7]]? add2 returns answer inside "[ ]"
>
>
># ...vs...
>
>
> print y # [1,2] - Ok
>
> y.append( add2(5) )
>
> print y # [1,2,[7]] - Ok; What I expected it to do
>
>
> I'm not clear what's going on when I do "list = list + function that
> returns list", i.e. why it's taking the 7 out of a list, where .append
> keeps it in...
>
> Thanks!
> --
> http://mail.python.org/mailman/listinfo/python-list




    ---------------------------------------------------------------------
   -    Andreas Jung                     http://www.andreas-jung.com   -
  -   EMail: andreas at andreas-jung.com                              -
   -            "Life is too short to (re)write parsers"               -
    ---------------------------------------------------------------------





More information about the Python-list mailing list