-1

My goal is to write a function, which receives a list of class objects and converts these into a list of lists, this is what I currently have:

def convertToListOfLists(toConvert):
    listOfLists = []
    temp = []
    for t in toConvert:
        temp.append(t.datenbestand)
        temp.append(t.aktenzeichen)
        temp.append(t.markendarstellung)
        temp.append(t.aktenzustand)
        listOfLists.append(temp)
        temp.clear()
    print(listOfLists)
    return listOfLists

Output: [[], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], []] while 'toConvert' holding 17 objects

If I move the print into the loop and print out my 'listOfLists' I can see that the objects are added to my 'listOfLists' correctly but as you can see, if I access 'listOfLists' outside the loop, 'listOfLists' only holds empty lists.

What am I missing?

Dethe
  • 23
  • 1
  • 1
  • 8

1 Answers1

1

You need to make a new temp everytime through the loop:

def convertToListOfLists(toConvert):
    listOfLists = []
    for t in toConvert:
        temp = []
        temp.append(t.datenbestand)
        temp.append(t.aktenzeichen)
        temp.append(t.markendarstellung)
        temp.append(t.aktenzustand)
        listOfLists.append(temp)
    print(listOfLists)
    return listOfLists
quamrana
  • 37,849
  • 12
  • 53
  • 71