This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: concatenation of Tuples
Type: behavior Stage: resolved
Components: Versions: Python 3.7
process
Status: closed Resolution: works for me
Dependencies: Superseder:
Assigned To: Nosy List: SilentGhost, ammar2, bruceblosser, steven.daprano
Priority: normal Keywords:

Created on 2020-02-15 23:24 by bruceblosser, last changed 2022-04-11 14:59 by admin. This issue is now closed.

Messages (7)
msg362036 - (view) Author: bruce blosser (bruceblosser) Date: 2020-02-15 23:24
The concatenation of two tuples into a third tuple, using the + command, causes an error if every member of each of the two tuples is NOT a string!  This does not appear to be documented ANYWHERE, and really causes a whole lot of head scratching and more than enough foul language!  :)

So how does one "add" two tuples together, to create a third tuple, if the members are not all strings?
msg362038 - (view) Author: Ammar Askar (ammar2) * (Python committer) Date: 2020-02-15 23:31
Are you trying to concatenate a single string? If so keep in mind you need a trailing comma:

>>> (1, 2) + (3, 4) + ('a',)
(1, 2, 3, 4, 'a')
>>> (1, 2) + (3, 4) + ('a')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: can only concatenate tuple (not "str") to tuple
msg362042 - (view) Author: Steven D'Aprano (steven.daprano) * (Python committer) Date: 2020-02-16 03:00
> The concatenation of two tuples into a third tuple, using the + command, causes an error if every member of each of the two tuples is NOT a string!

Works for me:

    py> ("Hello", 1, None) + (23, 19.5, "Goodbye")
    ('Hello', 1, None, 23, 19.5, 'Goodbye')

If you still think there is a bug here, please follow the advice given here

http://www.sscce.org/

before re-opening this ticket.
msg362056 - (view) Author: bruce blosser (bruceblosser) Date: 2020-02-16 08:45
read the advice...
Yes this does work:

  ("Hello", 1, None) + (23, 19.5, "Goodbye")
    ('Hello', 1, None, 23, 19.5, 'Goodbye')
because you are not creating a 3rd string!

but try this, and it will NOT work:

FatThing= [(5, 4, "First Place"),
           (6, 6, "Fifer Place"),
           (2, 2, "Slowr Place")]
print(FatThing)  #this works

FFThing = FatThing + ('22', '32', '55')  #this causes an error!

however if you change all the members to strings, it will work!!!
msg362057 - (view) Author: SilentGhost (SilentGhost) * (Python triager) Date: 2020-02-16 08:56
Bruce, error message says exactly why it doesn't work: you're trying to add a tuple to a list, and that doesn't work. There isn't a "third" string created anywhere, you're confused about the types of the objects. Also, please, do not re-open closed issues.
msg362063 - (view) Author: Steven D'Aprano (steven.daprano) * (Python committer) Date: 2020-02-16 09:35
[Bruce]
> but try this, and it will NOT work:
> 
> FatThing= [(5, 4, "First Place"),
>            (6, 6, "Fifer Place"),
>            (2, 2, "Slowr Place")]
> print(FatThing)  #this works
> 
> FFThing = FatThing + ('22', '32', '55')  #this causes an error!

That is correct, it should cause an error because you are trying to 
concatenate a list and a tuple. This is an easier way to show the same 
behaviour:

    [] + ()  # fails with TypeError

[Bruce]
> however if you change all the members to strings, it will work!!!

I'm afraid you are mistaken. It still fails, as it should.

py> FatThing = [("a", "b", "First Place"),
...             ("c", "d", "Fifer Place"),
...             ("e", "f", "Slowr Place")]
py> FFThing = FatThing + ('22', '32', '55')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: can only concatenate list (not "tuple") to list

Please take more care when trying to report what you think is a bug. 
Remember that Python is about 30 years old and there are tens or 
hundreds of thousands of people using it every single day. 99% of the 
time, anything you, or I, find that looks like a bug, is a bug in *our* 
code, not Python. Especially when it is something as basic and 
fundamental as tuple concatenation.
msg362064 - (view) Author: bruce blosser (bruceblosser) Date: 2020-02-16 09:49
ok -  well sorry, I am obviously in way over my head, and now very confused...  

I was just going by what was being said on a number of python web sites, including one where I am taking a class in intermediate python coding, and thought I was seeing a confiict between what i was being told, and what I was finding when running code.

so I will try not to come back here, unless I have some major problem, that seems more like a bug

thanks
bruce
History
Date User Action Args
2022-04-11 14:59:26adminsetgithub: 83822
2020-02-16 09:49:35bruceblossersetmessages: + msg362064
2020-02-16 09:35:59steven.dapranosetmessages: + msg362063
2020-02-16 08:56:47SilentGhostsetstatus: open -> closed


messages: + msg362057
nosy: + SilentGhost
2020-02-16 08:45:26bruceblossersetstatus: closed -> open

messages: + msg362056
2020-02-16 03:00:03steven.dapranosetstatus: open -> closed

nosy: + steven.daprano
messages: + msg362042

resolution: works for me
stage: resolved
2020-02-15 23:31:23ammar2setmessages: + msg362038
2020-02-15 23:30:22ammar2setmessages: - msg362037
2020-02-15 23:29:55ammar2setnosy: + ammar2
messages: + msg362037
2020-02-15 23:24:11bruceblossercreate