Thankyou.. but my problem is different than simply joining 2 lists and it is done now :).... <br><br><div class="gmail_quote">On Tue, Oct 23, 2012 at 11:38 AM, Joshua Landau <span dir="ltr"><<a href="mailto:joshua.landau.ws@gmail.com" target="_blank">joshua.landau.ws@gmail.com</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div class="im">On 23/10/2012, inshu chauhan <<a href="mailto:insideshoes@gmail.com">insideshoes@gmail.com</a>> wrote:<br>

> can we append a list with another list in Python ? using the normal routine<br>
> syntax but with a for loop ??<br>
<br>
</div>I assume you want to join two lists.<br>
<br>
You are corrrect that we can do:<br>
<br>
>>> start = [1, 2, 3, 4]<br>
>>> end = [5, 6, 7, 8]<br>
>>><br>
>>> for end_item in end:<br>
>>>     start.append(end_item)<br>
>>><br>
>>> print(start)<br>
[1, 2, 3, 4, 5, 6, 7, 8]<br>
>>><br>
<br>
However, it is markedly repetitive, no?<br>
This is a common enough operation that there is a shortcut to find out about it.<br>
<br>
If you want to find out what methods there are, try "help(...)". I<br>
can't stress this enough.<br>
<br>
>>> help(start)<br>
Help on list object:<br>
<br>
class list(object)<br>
 |  list() -> new empty list<br>
 |  list(iterable) -> new list initialized from iterable's items<br>
 |<br>
 |  Methods defined here:<br>
 |<br>
 |  __add__(...)<br>
 |      x.__add__(y) <==> x+y<br>
...<br>
 |  append(...)<br>
 |      L.append(object) -- append object to end<br>
...<br>
 |  extend(...)<br>
 |      L.extend(iterable) -- extend list by appending elements from<br>
the iterable<br>
...<br>
<br>
So list.extend seems to do exactly this!<br>
<br>
You can always check the documentation<br>
<<a href="http://docs.python.org/tutorial/datastructures.html" target="_blank">http://docs.python.org/tutorial/datastructures.html</a>>.<br>
An lo! The documentation says "start.extend(end)" is _equivilant_ to<br>
"start[len(start):] = end".<br>
<br>
Why?<br>
<br>
Well, this uses the slicing syntax.<br>
<br>
>>> start[:3]<br>
[1, 2, 3]<br>
>>> start[3:]<br>
[4]<br>
>>> start[2:3]<br>
[3]<br>
<br>
Wonderously, all these really say are "ranges" in the list. Hence, you<br>
can "put" lists in their place.<br>
<br>
"start[len(start):] = end" means "start[-1:] = end", so what you're<br>
doing is saying "the empty end part of the list is actually this new<br>
list". Hopefully that makes sense.<br>
<br>
Finally, there is another method. Instead of *changing* the list, you<br>
can make a new list which is equal to the others "added" together.<br>
<br>
>>> new = start + end<br>
<br>
_______________________________________________<br>
<br>
Theses methods all have their own upsides. If you want to change the<br>
list, use .extend(). If you want to change the list, but by putting<br>
the new list somewhere inside the "old" one, use slicing:<br>
<br>
>>> start = [1, 2, 3, 4]<br>
>>> end = [5, 6, 7, 8]<br>
>>><br>
>>> start[2:2] = end<br>
>>> print(start)<br>
[1, 2, 5, 6, 7, 8, 3, 4]<br>
<br>
Looping is good for when you want to generate the extra items as you go along.<br>
<br>
Finally, if you want to keep the old list or use these "inline", use "+".<br>
<br>
_______________________________________________<br>
<br>
Note that, being in the unfortunate position of "away from an<br>
interpreter", none of my examples are copy-pastes. Hence they may be<br>
wrong :/<br>
<br>
# Not checked for errors, typos and my "friends" messing with it.<br>
</blockquote></div><br>