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

inshu chauhan insideshoes at gmail.com
Tue Oct 23 05:15:48 EDT 2012


---------- Forwarded message ----------
From: Zero Piraeus <schesis at gmail.com>
Date: Tue, Oct 23, 2012 at 11:14 AM
Subject: Re: can we append a list with another list in Python ?
To: inshu chauhan <insideshoes at gmail.com>


:

On 23 October 2012 05:01, inshu chauhan <insideshoes at gmail.com> wrote:
> this is because this makes a single list out of 2 lists.. but I want to
retain both lists as lists within list....... I hope u got it ??
>
> my problem is for example :
>
> x = [1,2,3]
> y = [10, 20, 30]
>
> I want the output to be :
>
> [[1,2,3], [10, 20, 30]] .. a list within list
>
> then i want to process each list in the big list using another function
????

For the example above, you'd just do:

>>> x = [1, 2, 3]
>>> y = [10, 20, 30]
>>> z = [x, y]
>>> z
[[1, 2, 3], [10, 20, 30]]

... but presumably there'll be more than two, and you're creating them
in some kind of loop? In that case, append is fine. For example:

>>> result = []
>>> for word in "squee", "kapow", "vroom":
...     seq = list(word)  # or whatever you're doing to create the list
...     result.append(seq)
...
>>> result
[['s', 'q', 'u', 'e', 'e'], ['k', 'a', 'p', 'o', 'w'], ['v', 'r', 'o',
'o', 'm']]

By the way - you only replied to me. Do you mind if I forward this
back to the list?

 -[]z.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20121023/daa68584/attachment.html>


More information about the Python-list mailing list