Python Forum
What is the reason for this difference between lists and tuples?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
What is the reason for this difference between lists and tuples?
#1
I am a Python beginner, and recently I discovered a key difference between lists and tuples.

If I write something like:

list1 = list1.append(list2)
...then list1 becomes a "NoneType" object. In other words if you pass an appended list to its own name, then it is replaced with a NoneType object.

This isn't the case with tuples though...is there a reason for this?
Reply
#2
first of all tuple has no attribute append.
>>> x = (1,2)
>>> y=(3,4)
>>> x.append(y)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'tuple' object has no attribute 'append'
list1.append(list2) will append list2 the to the list1 in-place, i.e. what you should do

>>> list1=[1,2]
>>> list2=[3,4]
>>> list1.append(list2)
>>> list1
[1, 2, [3, 4]]
as you can see list2 is added as 3-rd element in the list1.
what I think you want to do is to use extend, instead of append

>>> list1=[1,2]
>>> list2=[3,4]
>>> list1.extend(list2)
>>> list1
[1, 2, 3, 4]
Reply
#3
Sorry, I meant that this difference occurs when you use the __add__ method on a tuple, which I consider to be the equivalent of append for tuples.
Reply
#4
__add__ is internal implementation that is called by + operator (i.e. concatenation)
https://docs.python.org/3/library/stdtyp...l#typesseq
read carefully note 6 and 3rd item in the bullet list
Quote:Concatenating immutable sequences always results in a new object. This means that building up a sequence by repeated concatenation will have a quadratic runtime cost in the total sequence length. To get a linear runtime cost, you must switch to one of the alternatives below:

if concatenating str objects, you can build a list and use str.join() at the end or else write to an io.StringIO instance and retrieve its value when complete
if concatenating bytes objects, you can similarly use bytes.join() or io.BytesIO, or you can do in-place concatenation with a bytearray object. bytearray objects are mutable and have an efficient overallocation mechanism
if concatenating tuple objects, extend a list instead
for other types, investigate the relevant class documentation

apart from performance issue that is discussed, note that concatenating tuple is equivalent of extending a list, but extending the list has better performance
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  coma separator is printed on a new line for some reason tester_V 4 505 Feb-02-2024, 06:06 PM
Last Post: tester_V
  Output difference from 2 lists of different sizes with words gracenz 5 1,352 Sep-02-2022, 05:09 PM
Last Post: Larz60+
  Split dict of lists into smaller dicts of lists. pcs3rd 3 2,403 Sep-19-2020, 09:12 AM
Last Post: ibreeden
  Reason of the output himanshub7 2 2,191 Apr-28-2020, 10:44 AM
Last Post: TomToad
  Code used to work, not anymore for no apparent reason? chicagoxjl 1 1,907 Jan-08-2020, 05:05 PM
Last Post: jefsummers
  Syntax error for a reason I don't understand DanielCook 2 2,455 Aug-07-2019, 11:49 AM
Last Post: buran
  sort lists of lists with multiple criteria: similar values need to be treated equal stillsen 2 3,303 Mar-20-2019, 08:01 PM
Last Post: stillsen
  "invalid syntax" for no apparent reason wardancer84 2 7,182 Oct-03-2018, 11:57 AM
Last Post: wardancer84
  for some reason this isnt working lokchi2017 3 2,719 Aug-06-2018, 12:24 PM
Last Post: perfringo
  My program subtracts fractions, but for some reason isn't simplifying them RedSkeleton007 9 5,942 Mar-03-2018, 11:45 AM
Last Post: Gribouillis

Forum Jump:

User Panel Messages

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