Ask Your Question
0

join lists?

asked 2012-02-03 17:21:13 +0200

this post is marked as community wiki

This post is a wiki. Anyone with karma >750 is welcome to improve it.

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, Francesco

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
1

answered 2012-02-03 17:31:26 +0200

this post is marked as community wiki

This post is a wiki. Anyone with karma >750 is welcome to improve it.

You 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.

edit flag offensive delete link more

Comments

2

In other words, lists form a semigroup. If you want subtraction you have to go through the Grothendiek construction....

Volker Braun gravatar imageVolker Braun ( 2012-02-03 18:45:33 +0200 )edit

In fact, it looks like they're a monoid - sage: [1,2,3]+[] [1, 2, 3]

kcrisman gravatar imagekcrisman ( 2012-02-03 20:16:30 +0200 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

Stats

Asked: 2012-02-03 17:21:13 +0200

Seen: 3,960 times

Last updated: Feb 03 '12