ASKSAGE: Sage Q&A Forum - Individual question feedhttp://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Fri, 03 Feb 2012 13:16:30 -0600join lists?http://ask.sagemath.org/question/8696/join-lists/Hello! Is there a way to join two lists? For example,
v=[1,2,3];
w=[4,5,6];
how can I obtain the list z=[1,2,3,4,5,6]?
Thank you very much,
FrancescoFri, 03 Feb 2012 10:21:13 -0600http://ask.sagemath.org/question/8696/join-lists/Answer by DSM for <p>Hello! Is there a way to join two lists? For example,
v=[1,2,3];
w=[4,5,6];</p>
<p>how can I obtain the list z=[1,2,3,4,5,6]?
Thank you very much,
Francesco</p>
http://ask.sagemath.org/question/8696/join-lists/?answer=13244#post-id-13244You can simply add them:
sage: a = [1,2,3]
sage: b = [4,5,6]
sage: z = a+b
sage: z
[1, 2, 3, 4, 5, 6]
Note that you can't subtract them, though:
sage: a-b
Traceback (most recent call last):
...
TypeError: unsupported operand type(s) for -: 'list' and 'list'
This is basically because it's not obvious what to do with [1,2,3]-[4,5], and a fundamental Python principle is to resist the temptation to guess in the face of ambiguity.
It might be helpful to work through [a Python tutorial](http://docs.python.org/tutorial/).Fri, 03 Feb 2012 10:31:26 -0600http://ask.sagemath.org/question/8696/join-lists/?answer=13244#post-id-13244Comment by Volker Braun for <p>You can simply add them:</p>
<pre><code>sage: a = [1,2,3]
sage: b = [4,5,6]
sage: z = a+b
sage: z
[1, 2, 3, 4, 5, 6]
</code></pre>
<p>Note that you can't subtract them, though:</p>
<pre><code>sage: a-b
Traceback (most recent call last):
...
TypeError: unsupported operand type(s) for -: 'list' and 'list'
</code></pre>
<p>This is basically because it's not obvious what to do with [1,2,3]-[4,5], and a fundamental Python principle is to resist the temptation to guess in the face of ambiguity. </p>
<p>It might be helpful to work through <a href="http://docs.python.org/tutorial/">a Python tutorial</a>.</p>
http://ask.sagemath.org/question/8696/join-lists/?comment=20351#post-id-20351In other words, lists form a semigroup. If you want subtraction you have to go through the Grothendiek construction....Fri, 03 Feb 2012 11:45:33 -0600http://ask.sagemath.org/question/8696/join-lists/?comment=20351#post-id-20351Comment by kcrisman for <p>You can simply add them:</p>
<pre><code>sage: a = [1,2,3]
sage: b = [4,5,6]
sage: z = a+b
sage: z
[1, 2, 3, 4, 5, 6]
</code></pre>
<p>Note that you can't subtract them, though:</p>
<pre><code>sage: a-b
Traceback (most recent call last):
...
TypeError: unsupported operand type(s) for -: 'list' and 'list'
</code></pre>
<p>This is basically because it's not obvious what to do with [1,2,3]-[4,5], and a fundamental Python principle is to resist the temptation to guess in the face of ambiguity. </p>
<p>It might be helpful to work through <a href="http://docs.python.org/tutorial/">a Python tutorial</a>.</p>
http://ask.sagemath.org/question/8696/join-lists/?comment=20350#post-id-20350In fact, it looks like they're a monoid -
sage: [1,2,3]+[]
[1, 2, 3]Fri, 03 Feb 2012 13:16:30 -0600http://ask.sagemath.org/question/8696/join-lists/?comment=20350#post-id-20350