>> [1,2,3] + (4,5,6) Tr...">

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

Python doesn't 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 to providing such support in a language? Note that I would expect this to be symmetric: [1, 2] + (3, 4)

and the (1, 2) + [3, 4]

new list will be evaluated [1, 2, 3, 4]

. My consideration is that as soon as someone applies the + operator to a set of tuples and lists, they will probably do it again (possibly in the same expression), so we could provide a list as well to avoid additional conversions ...

Here is my motivation for this question.

It often happens that I have small collections that I prefer to store as tuples to avoid accidental modification and help performance. Then I need to combine such tuples with lists, and converting each of them to a 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 ones are tuples and which are lists. So I need to either 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 chain

can also be slower (since it has to iterate through the inputs of an element at a time).

+3


source to share


3 answers


This is not supported because the operator +

must be symmetric. What type of return do you expect? Python Zen includes a rule

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

      

However the following works:



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

      

There is no ambiguity about which type to use here.

+10


source


Why doesn't python support adding another type: The simple answer is that they are of different types, what if you try to add iterable and expect a list? I myself would like to return another iterable. Also consider ['a','b']+'cd'

what should be the exit? given that explicit is better than implicit , all such implicit conversions are prohibited.

To overcome this limitation, use the list method extend

to add any iterative file, eg.

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

      

If you need to add many lists / 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]

      

+2


source


You can use the operator +=

if it 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 as you mentioned, readability can suffer:

>>> list((1,2,3)) + list((1,2,3))
[1, 2, 3, 1, 2, 3]

      

+1


source







All Articles