Python Forum
concatenating list with tuple
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
concatenating list with tuple
#1
i can do mylist+=mytuple but i cannot do mylist=mylist+mytuple. why is that?

lt1/forums /home/forums 1> py3
Python 3.5.2 (default, Nov 23 2017, 16:37:01) 
[GCC 5.4.0 20160609] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> a=[1,2,3]
>>> b=(4,5,6)
>>> a+=b
>>> a=a+b
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: can only concatenate list (not "tuple") to list
>>>
lt1/forums /home/forums 1> py2
Python 2.7.12 (default, Dec  4 2017, 14:50:18) 
[GCC 5.4.0 20160609] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> a=[1,2,3]
>>> b=(4,5,6)
>>> a+=b
>>> a=a+b
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: can only concatenate list (not "tuple") to list
>>>
and mylist.extend(mytuple) also works.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#2
One calls __add__ (+) the other __iadd__ (+=). The former creates a new list from concatination of two lists, the latter appends (i.e. in-place) items from an iterable, which is how extend works. Thus, the former is looking to combine lists, and the latter to extend with any viable iterable.
I am trying to help you, really, even if it doesn't always seem that way
Reply
#3
i assume __add__ came first, probably from GvR as part of the original design before the simplification of iterators. and __iadd__ came later, after the simplification of iterators and __add__ hasn't been updated, yet, to have consistency. maybe in Python4? maybe in some other implementation?
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#4
(Sep-27-2018, 08:27 PM)Skaperen Wrote: i assume __add__ came first, probably from GvR as part of the original design before the simplification of iterators. and __iadd__ came later, after the simplification of iterators and __add__ hasn't been updated, yet, to have consistency. maybe in Python4? maybe in some other implementation?

Er, huh? You can overide if you like. Seems consistent to me though. Changing the standard implementation of __add__ would cause a lot of problems I suspect.
I am trying to help you, really, even if it doesn't always seem that way
Reply
#5
seems inconsistent to me. define addition as you will, but there is no real reason for it to be different between + and +=. how it works in a named method is something else.

all the reasoning i keep reading about comes down to implementation and that's never a reason to limit a definition of how things should work, especially when something else shows that it can be done.

the __add__ method can be made to do what __iadd__ does, or simply just call __add__ to do it, by creating the new object, applying __iadd__ to that new object or by just doing what __iadd__ does, and returning that new object. since Python does not allow a retroactive method replacement for list, this cannot be "fixed" in Python code.

now here is where it gets fun. += works on tuples. treating a tuple as an iterator is easy enough. extending it is another thing. and += also works for strings.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#6
my view that it is inconsistent is in reference to the effect as seen by use of the language, not by how it is implemented. if someone changed things so that both + and += supported concatenating a list on the left and a tuple on the right, then i would say it is consistent. what would you call it?

whatever is done for += should call __add__ for the implementation of the add step, regardless of the type or class. then it only takes a new class implementing __add__ and then both + and += will work. it should be like this for all operators that appending = makes them an "operate and assign". == is the only different case i can think of at the moment.

this also makes the language simpler. then it can be taught that by appending (only once) = to the operator, except for comparison operators (== >= <=), works like "do the first operator and assign to LHS".
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#7
You cannot implement __iadd__ for immutable types as in place changes are not supported. That is why the operator in question falls back to __add__ when handling such and therefore is merely syntactic sugar. Something to debate on the pydev mailing list if you want to debate further.

Hopefully the OP has the answer they needed appropriate to a homework question.
I am trying to help you, really, even if it doesn't always seem that way
Reply
#8
it wasn't homework. i don't do any homework. oh wait. i work at home (managing cloud setups ... like a virtual sysadmin). i don't have to work in the data center. so i just work at home.

post #1 was just one of my idiotic rants.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  using > < for tuple , list,... akbarza 3 402 Feb-05-2024, 01:18 PM
Last Post: deanhystad
Question mypy unable to analyse types of tuple elements in a list comprehension tomciodev 1 427 Oct-17-2023, 09:46 AM
Last Post: tomciodev
  Change font in a list or tuple apffal 4 2,635 Jun-16-2023, 02:55 AM
Last Post: schriftartenio
  search a list or tuple for a specific type ot class Skaperen 8 1,855 Jul-22-2022, 10:29 PM
Last Post: Skaperen
  why is my list a tuple CompleteNewb 7 2,214 Mar-17-2022, 10:09 PM
Last Post: CompleteNewb
  in a list or tuple Skaperen 6 78,504 May-16-2021, 09:59 PM
Last Post: Skaperen
  Create SQLite columns from a list or tuple? snakes 6 8,525 May-04-2021, 12:06 PM
Last Post: snakes
  What type of *data* is the name of a list/tuple/dict, etc? alloydog 9 4,260 Jan-30-2021, 07:11 AM
Last Post: alloydog
  code with no tuple gets : IndexError: tuple index out of range Aggam 4 2,729 Nov-04-2020, 11:26 AM
Last Post: Aggam
  concatenating 2 items at a time in a python list K11 3 2,247 Oct-21-2020, 09:34 AM
Last Post: buran

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020