Why can't I add a tuple to a list with the '+' operator in Python?

Question:

Python not support adding a tuple to a list:

>>> [1,2,3] + (4,5,6)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: can only concatenate list (not "tuple") to list

What are the disadvantages for providing such a support in the language? Note that I would expect this to be symmetric: [1, 2] + (3, 4) and (1, 2) + [3, 4] would both evaluate to a brand-new list [1, 2, 3, 4]. My rationale is that once someone applied operator + to a mix of tuples and lists, they are likely to do it again (very possibly in the same expression), so we might as well provide the list to avoid extra conversions.

Here’s my motivation for this question.

It happens quite often that I have small collections that I prefer to store as tuples to avoid accidental modification and to help performance. I then need to combine such tuples with lists, and having to convert each of them to list makes for very ugly code.

Note that += or extend may work in simple cases. But in general, when I have an expression

columns = default_columns + columns_from_user + calculated_columns

I don’t know which of these are tuples and which are lists. So I either have to convert everything to lists:

columns = list(default_columns) + list(columns_from_user) + list(calculated_columns)

Or use itertools:

columns = list(itertools.chain(default_columns, columns_from_user, calculated_columns))

Both of these solutions are uglier than a simple sum; and the chain may also be slower (since it must iterate through the inputs an element at a time).

Asked By: max

||

Answers:

You can use the += operator, if that helps:

>>> x = [1,2,3]
>>> x += (1,2,3)
>>> x
[1, 2, 3, 1, 2, 3]

You can also use the list constructor explicitly, but like you mentioned, readability might suffer:

>>> list((1,2,3)) + list((1,2,3))
[1, 2, 3, 1, 2, 3]
Answered By: jterrace

Why python doesn’t support adding different type: simple answer is that they are of different types, what if you try to add a iterable and expect a list out? I myself would like to return another iterable. Also consider ['a','b']+'cd' what should be the output? considering explicit is better than implicit all such implicit conversions are disallowed.

To overcome this limitation use extend method of list to add any iterable e.g.

l = [1,2,3]
l.extend((4,5,6))

If you have to add many list/tuples write a function

def adder(*iterables):
    l = []
    for i in iterables:
        l.extend(i)
    return l

print adder([1,2,3], (3,4,5), range(6,10))

output:

[1, 2, 3, 3, 4, 5, 6, 7, 8, 9]
Answered By: Anurag Uniyal

This is not supported because the + operator is supposed to be symmetric. What return type would you expect? The Python Zen includes the rule

In the face of ambiguity, refuse the temptation to guess.

The following works, though:

a = [1, 2, 3]
a += (4, 5, 6)

There is no ambiguity what type to use here.

Answered By: Sven Marnach