Subtracting 2 lists in Python

Joan Venge picture Joan Venge · Feb 11, 2009 · Viewed 157.4k times · Source

Right now I have vector3 values represented as lists. is there a way to subtract 2 of these like vector3 values, like

[2,2,2] - [1,1,1] = [1,1,1]

Should I use tuples?

If none of them defines these operands on these types, can I define it instead?

If not, should I create a new vector3 class?

Answer

UncleZeiv picture UncleZeiv · Feb 11, 2009

If this is something you end up doing frequently, and with different operations, you should probably create a class to handle cases like this, or better use some library like Numpy.

Otherwise, look for list comprehensions used with the zip builtin function:

[a_i - b_i for a_i, b_i in zip(a, b)]