Python Forum
List of objects with sublist
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
List of objects with sublist
#1
Suppose I have the following code

newList = []
for i in range(10):
    newList.append([])

for i in range(len(newList)):
    newList[i].append(1)

>>> newList[0]
[1]
If however instead of having a list of lists, I have a list of objects containing a list attribute, the output is not the one I would expect:

class testobj:
  def __init__(tst,lst = []):
    tst.lst = lst

newList = []
for i in range(10):
    newList.append(testobj())


for i in range(len(newList)):
    newList[i].lst.append(1)
    
>>> newList[0].lst
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1]
Can someone explain why? How would I make the list of objects behave correctly?
Thanks
Reply
#2
you are using mutable default argument in the __init__

Read https://docs.python-guide.org/writing/go...-arguments
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#3
as a side note - don't use for i in range(len(newList)):, read https://python-forum.io/Thread-Basic-Nev...n-sequence
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#4
Ok thanks, but what's the syntax to do this in the constructor of the object?

EDT:

got it
class testobj:
  def __init__(tst,lst = None):
    tst.lst = []
Reply
#5
if you do it like this, then it doesn't make sense to have lst argument at all - you can remove it

class testobj:
  def __init__(tst):
    tst.lst = []
by the way, convention is to use self. It's OK to use something different like tst but it make it more difficult for other developers to read
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Creating list of lists, with objects from lists sgrinderud 7 1,563 Oct-01-2022, 07:15 PM
Last Post: Skaperen
Question Keyword to build list from list of objects? pfdjhfuys 3 1,499 Aug-06-2022, 11:39 PM
Last Post: Pedroski55
  How to store the resulting Doc objects into a list named A xinyulon 1 1,853 Mar-08-2022, 11:49 PM
Last Post: bowlofred
  Use one list as search key for another list with sublist of list jc4d 4 2,107 Jan-11-2022, 12:10 PM
Last Post: jc4d
  Grouping and sum of a list of objects Otbredbaron 1 3,131 Oct-23-2021, 01:42 PM
Last Post: Gribouillis
Question Sublist/ Subarray into string Python SantiagoPB 2 2,096 Apr-23-2021, 07:03 PM
Last Post: SantiagoPB
  Passing List of Objects in Command Line Python usman 7 3,086 Sep-27-2020, 03:45 PM
Last Post: ndc85430
  How to create and define in one line a 2D list of class objects in Python T2ioTD 1 1,988 Aug-14-2020, 12:37 PM
Last Post: Yoriz
  Organizing list of objects by attribute scarney1988 3 2,155 Mar-11-2020, 03:55 PM
Last Post: scarney1988
  Insert into sublist if sublist is not having same no of records. parthi1705 10 4,380 May-28-2019, 12:01 PM
Last Post: perfringo

Forum Jump:

User Panel Messages

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