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


Messages In This Thread
concatenating list with tuple - by Skaperen - Sep-23-2018, 08:34 PM
RE: concatenating list with tuple - by gruntfutuk - Sep-27-2018, 11:21 AM
RE: concatenating list with tuple - by Skaperen - Sep-27-2018, 08:27 PM
RE: concatenating list with tuple - by gruntfutuk - Sep-27-2018, 09:30 PM
RE: concatenating list with tuple - by Skaperen - Sep-27-2018, 11:48 PM
RE: concatenating list with tuple - by Skaperen - Sep-28-2018, 01:36 AM
RE: concatenating list with tuple - by gruntfutuk - Sep-28-2018, 05:48 AM
RE: concatenating list with tuple - by Skaperen - Sep-28-2018, 05:55 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  using > < for tuple , list,... akbarza 3 510 Feb-05-2024, 01:18 PM
Last Post: deanhystad
Question mypy unable to analyse types of tuple elements in a list comprehension tomciodev 1 507 Oct-17-2023, 09:46 AM
Last Post: tomciodev
  Change font in a list or tuple apffal 4 2,718 Jun-16-2023, 02:55 AM
Last Post: schriftartenio
  search a list or tuple for a specific type ot class Skaperen 8 1,978 Jul-22-2022, 10:29 PM
Last Post: Skaperen
  why is my list a tuple CompleteNewb 7 2,298 Mar-17-2022, 10:09 PM
Last Post: CompleteNewb
  in a list or tuple Skaperen 6 95,546 May-16-2021, 09:59 PM
Last Post: Skaperen
  Create SQLite columns from a list or tuple? snakes 6 8,801 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,414 Jan-30-2021, 07:11 AM
Last Post: alloydog
  code with no tuple gets : IndexError: tuple index out of range Aggam 4 2,869 Nov-04-2020, 11:26 AM
Last Post: Aggam
  concatenating 2 items at a time in a python list K11 3 2,383 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