Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #31924

Re: can we append a list with another list in Python ?

Path csiph.com!usenet.pasdenom.info!news.albasani.net!feeder.erje.net!newsfeed.xs4all.nl!newsfeed5.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail
Return-Path <joshua.landau.ws@gmail.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.001
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'syntax': 0.03; 'subject:Python': 0.05; 'method.': 0.05; 'append': 0.07; 'python': 0.09; '[1,': 0.09; 'errors,': 0.09; 'messing': 0.09; 'shortcut': 0.09; 'cc:addr:python-list': 0.10; '<>.': 0.11; 'assume': 0.11; 'enough.': 0.16; 'iterable': 0.16; 'looping': 0.16; 'slicing:': 0.16; 'syntax.': 0.16; 'wrote:': 0.17; 'examples': 0.18; 'saying': 0.18; '>>>': 0.18; 'putting': 0.20; 'together.': 0.21; 'finally,': 0.22; 'defined': 0.22; 'cc:2**0': 0.23; 'elements': 0.23; 'seems': 0.23; 'somewhere': 0.24; 'cc:no real name:2**0': 0.24; 'cc:addr:python.org': 0.25; 'header:In-Reply-To:1': 0.25; 'common': 0.26; 'extend': 0.26; 'message-id:@mail.gmail.com': 0.27; 'subject:list': 0.28; 'along.': 0.29; 'appending': 0.29; 'initialized': 0.29; 'routine': 0.29; '"the': 0.29; 'class': 0.29; 'subject: ?': 0.30; 'checked': 0.30; 'lists': 0.31; 'url:python': 0.32; 'received:74.125.82': 0.33; 'says': 0.33; 'hopefully': 0.33; 'equal': 0.33; 'another': 0.33; 'that,': 0.34; "can't": 0.34; 'received:google.com': 0.34; 'wrong': 0.34; 'list': 0.35; 'lists.': 0.35; 'doing': 0.35; 'there': 0.35; 'list.': 0.35; 'skip:_ 40': 0.35; 'really': 0.36; 'but': 0.36; 'received:74.125': 0.36; 'url:org': 0.36; 'subject:with': 0.36; 'enough': 0.36; 'one,': 0.37; 'two': 0.37; 'uses': 0.37; 'being': 0.37; 'subject:: ': 0.38; 'object': 0.38; 'url:docs': 0.38; 'instead': 0.39; 'list,': 0.39; 'skip:" 10': 0.40; 'header:Received:5': 0.40; 'help': 0.40; 'end': 0.40; 'here:': 0.62; 'object:': 0.84; 'why?': 0.84; 'do:': 0.91; 'this!': 0.93; 'url:tutorial': 0.93
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :cc:content-type; bh=D5M7JQkBUdu3SG4ZYVX0m8iwViPKEnbF4f3beO7jtss=; b=lrn/6DcoITWuQC08pcHQ9cIR4J+PD3aLl21vJ4CeC53FQ070K7CYB83COGTuiYNNH7 CCNbVxCW2vD9UsSCzACKfAuL1wC3u/XM0d/YWF1PfVMzaPZSmTSwefa5fNY+Dd/6WV0M A9jFkGeEj/GhhVAi7TpTHUhDy0n8fL5TBchr9NZNScaFxOnthh8MhNxmKdeoLyruNwtr SW2YuUPQadE4aQ6fDKjyasiD0IYzqGp8y13iPRSwAK7Pl2h5NlR80Rh0DEYTcv+6pxDw eT+rfKAzNg5Is4dAMTERvzEHpMmAVAuvfMo+c0t7e1/F8gkRFQXHy16RQMJ0UrTVW1SG kpuA==
MIME-Version 1.0
In-Reply-To <CAFqGZRHGgN_tEinBku9cj78D_VawZgP3F8k5+FKen0mW3AeVbg@mail.gmail.com>
References <CAFqGZRHGgN_tEinBku9cj78D_VawZgP3F8k5+FKen0mW3AeVbg@mail.gmail.com>
Date Tue, 23 Oct 2012 10:38:23 +0100
Subject Re: can we append a list with another list in Python ?
From Joshua Landau <joshua.landau.ws@gmail.com>
To inshu chauhan <insideshoes@gmail.com>
Content-Type text/plain; charset=ISO-8859-1
Cc python-list@python.org
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.15
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <http://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.2657.1350985110.27098.python-list@python.org> (permalink)
Lines 98
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1350985110 news.xs4all.nl 6886 [2001:888:2000:d::a6]:48184
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:31924

Show key headers only | View raw


On 23/10/2012, inshu chauhan <insideshoes@gmail.com> wrote:
> can we append a list with another list in Python ? using the normal routine
> syntax but with a for loop ??

I assume you want to join two lists.

You are corrrect that we can do:

>>> start = [1, 2, 3, 4]
>>> end = [5, 6, 7, 8]
>>>
>>> for end_item in end:
>>>     start.append(end_item)
>>>
>>> print(start)
[1, 2, 3, 4, 5, 6, 7, 8]
>>>

However, it is markedly repetitive, no?
This is a common enough operation that there is a shortcut to find out about it.

If you want to find out what methods there are, try "help(...)". I
can't stress this enough.

>>> help(start)
Help on list object:

class list(object)
 |  list() -> new empty list
 |  list(iterable) -> new list initialized from iterable's items
 |
 |  Methods defined here:
 |
 |  __add__(...)
 |      x.__add__(y) <==> x+y
...
 |  append(...)
 |      L.append(object) -- append object to end
...
 |  extend(...)
 |      L.extend(iterable) -- extend list by appending elements from
the iterable
...

So list.extend seems to do exactly this!

You can always check the documentation
<http://docs.python.org/tutorial/datastructures.html>.
An lo! The documentation says "start.extend(end)" is _equivilant_ to
"start[len(start):] = end".

Why?

Well, this uses the slicing syntax.

>>> start[:3]
[1, 2, 3]
>>> start[3:]
[4]
>>> start[2:3]
[3]

Wonderously, all these really say are "ranges" in the list. Hence, you
can "put" lists in their place.

"start[len(start):] = end" means "start[-1:] = end", so what you're
doing is saying "the empty end part of the list is actually this new
list". Hopefully that makes sense.

Finally, there is another method. Instead of *changing* the list, you
can make a new list which is equal to the others "added" together.

>>> new = start + end

_______________________________________________

Theses methods all have their own upsides. If you want to change the
list, use .extend(). If you want to change the list, but by putting
the new list somewhere inside the "old" one, use slicing:

>>> start = [1, 2, 3, 4]
>>> end = [5, 6, 7, 8]
>>>
>>> start[2:2] = end
>>> print(start)
[1, 2, 5, 6, 7, 8, 3, 4]

Looping is good for when you want to generate the extra items as you go along.

Finally, if you want to keep the old list or use these "inline", use "+".

_______________________________________________

Note that, being in the unfortunate position of "away from an
interpreter", none of my examples are copy-pastes. Hence they may be
wrong :/

# Not checked for errors, typos and my "friends" messing with it.

Back to comp.lang.python | Previous | Next | Find similar | Unroll thread


Thread

Re: can we append a list with another list in Python ? Joshua Landau <joshua.landau.ws@gmail.com> - 2012-10-23 10:38 +0100

csiph-web