[Tutor] append vs list addition

Dave Angel davea at davea.name
Tue May 6 22:21:24 CEST 2014


Denis Heidtmann <denis.heidtmann at gmail.com> Wrote in message:
> On Sun, May 4, 2014 at 6:44 PM, Dave Angel <davea at davea.name> wrote:
>> C Smith <illusiontechniques at gmail.com> Wrote in message:
>>> Sorry.
>>>
>>> I meant for example:
>>> list1 = [1,2,3]
>>> list2 = [3,4,5]
>>>
>>> newList = list1 + list2
>>>
>>> versus
>>>
>>> for x in list2:
>>>     list1.append(x)
>>>
>>> Which is the preferred way to add elements from one list to another?
>>
>> Thank you for switching to text mail.
>>
>> These examples still aren't equivalent.  But in any similar
>>  example,  if list2 is type list, then avoid the list. Use either
>>  extend, or the equivalent += . And if you aren't permitted to
>>  change list1, you should use +=
> ....
>> DaveA
>>
> 
> Lurking here.  I am confused by "avoid the list".  What does that mean?
>  Also, you say extend and += are equivalent, yet say " if you aren't
> permitted to change list1, you should use +="  If they are equivalent,
> why choose one over the other?  Doesn't += always change the left-hand
> side?
> 
> Not being critical--just confused.



You're right to be confused;  my fingers were confused typing my
 last sentence.  It should have ended:
       ... you should use + .

Likewise the previous thought should have said:
     
 But in any similar
 example,  if list2 is type list, then
 avoid the loop. 

I've been trying to use a tablet to run a newsreader,  and between
 bugs in the reader and an over enthusiastic spell correction, 
 I'm ready to give up.

-- 
DaveA



More information about the Tutor mailing list