append() is overwriting the previous data he has written instead of just adding at the end

Question:

i’m wondering why the function append() is overwritting the elements given in a list that he has to write one after another.

I’m really lost here.

So here is the list of elements (I use fake identity names)

list = [{'Objet': 'premier', 'ne pas ': 'supprimer', "don't": 'delete'}, {'name': 'Bridoux', 'first_name': 'Justin', 'birthday': '14/05/2000', 'note': 'aucune', 'player_id': 1, 'total_score': 0, 'tournament_score': 0}, {'name': 'Clerico', 'first_name': 'Leonard', 'birthday': '07/08/1969', 'note': 'aucune', 'player_id': 2, 'total_score': 0, 'tournament_score': 0}, {'name': 'Boutroux', 'first_name': 'Yvon', 'birthday': '29/04/1984', 'note': 'aucune', 'player_id': 3, 'total_score': 0, 'tournament_score': 0}, {'name': 'Badeaux', 'first_name': 'Georges', 'birthday': '17/02/1962', 'note': 'aucune', 'player_id': 4, 'total_score': 0, 'tournament_score': 0}, {'name': 'Lajoie', 'first_name': 'Etienne', 'birthday': '16/06/1978', 'note': 'aucune', 'player_id': 5, 'total_score': 0, 'tournament_score': 0}, {'name': 'Foucher', 'first_name': 'Vincent', 'birthday': '08/01/1990', 'note': 'aucune', 'player_id': 6, 'total_score': 0, 'tournament_score': 0}, {'name': 'Coordonnier', 'first_name': 'Pierre', 'birthday': '20/10/1992', 'note': 'aucune', 'player_id': 7, 'total_score': 0, 'tournament_score': 0}, {'name': 'Chaucard', 'first_name': 'Alex', 'birthday': '10/07/2001', 'note': 'aucune', 'player_id': 8, 'total_score': 0, 'tournament_score': 0}, {'name': 'Marchand', 'first_name': 'Enzo', 'birthday': '06/06/1995', 'note': 'aucune', 'player_id': 9, 'total_score': 0, 'tournament_score': 0}, {'name': 'Flantier', 'first_name': 'Noel', 'birthday': '25/12/1960', 'note': 'OSS 117', 'player_id': 10, 'total_score': 0, 'tournament_score': 0}]

And here is the code that iterate over the list of dictionnaries to get only names, first_name, and player_id.

data = [["Nom", "Prénom", "ID Joueur"]]
data_row = []

for player in range(len(list)):
            for key in list[player]:
                if key == 'name':
                    data_row.append(list[player]['name'])
                elif key == 'first_name':
                    data_row.append(list[player]['first_name'])
                elif key == 'player_id':
                    data_row.append(list[player]['player_id'])
                    print(data_row)
                    data.append(data_row)
                    print(data)
                    data_row.clear()

I should have a list of lists with 3 attributes in each of them(name, first name, and player id).

But has you can see it’s overwriting the previous list that has been given instead of just adding at the end of the list.

['Bridoux', 'Justin', 1]
[['Nom', 'Prénom', 'ID Joueur'], ['Bridoux', 'Justin', 1]]
['Clerico', 'Leonard', 2]
[['Nom', 'Prénom', 'ID Joueur'], ['Clerico', 'Leonard', 2], ['Clerico', 'Leonard', 2]]
['Boutroux', 'Yvon', 3]
[['Nom', 'Prénom', 'ID Joueur'], ['Boutroux', 'Yvon', 3], ['Boutroux', 'Yvon', 3], ['Boutroux', 'Yvon', 3]]

I’ve tryied to do it with data += data_row but then it’s not putting the [] so I no longer have lists inside.

Why does it work like this ? and it’s not removing the first element that as been declared at first as you can see.

The expected result would be :

[['Nom', 'Prénom', 'ID Joueur'], ['Bridoux', 'Justin', 1], ['Clerico', 'Leonard', 2], ['Boutroux', 'Yvon', 3]]
Asked By: Matteo

||

Answers:

you can try using

list=[{'name': 'Clerico', 'first_name': 'Leonard', 'birthday': '07/08/1969', 'note': 'aucune', 'player_id': 2, 'total_score': 0, 'tournament_score': 0}, {'name': 'Boutroux', 'first_name': 'Yvon', 'birthday': '29/04/1984', 'note': 'aucune', 'player_id': 3, 'total_score': 0, 'tournament_score': 0}, {'name': 'Badeaux', 'first_name': 'Georges', 'birthday': '17/02/1962', 'note': 'aucune', 'player_id': 4, 'total_score': 0, 'tournament_score': 0}, {'name': 'Lajoie', 'first_name': 'Etienne', 'birthday': '16/06/1978', 'note': 'aucune', 'player_id': 5, 'total_score': 0, 'tournament_score': 0}, {'name': 'Foucher', 'first_name': 'Vincent', 'birthday': '08/01/1990', 'note': 'aucune', 'player_id': 6, 'total_score': 0, 'tournament_score': 0}, {'name': 'Coordonnier', 'first_name': 'Pierre', 'birthday': '20/10/1992', 'note': 'aucune', 'player_id': 7, 'total_score': 0, 'tournament_score': 0}, {'name': 'Chaucard', 'first_name': 'Alex', 'birthday': '10/07/2001', 'note': 'aucune', 'player_id': 8, 'total_score': 0, 'tournament_score': 0}, {'name': 'Marchand', 'first_name': 'Enzo', 'birthday': '06/06/1995', 'note': 'aucune', 'player_id': 9, 'total_score': 0, 'tournament_score': 0}, {'name': 'Flantier', 'first_name': 'Noel', 'birthday': '25/12/1960', 'note': 'OSS 117', 'player_id': 10, 'total_score': 0, 'tournament_score': 0}]

data = [["Nom", "Prénom", "ID Joueur"]]
data_row = []

for player in range(len(list)):
            for key in list[player]:
                if key == 'name':
                    data_row.append(list[player]['name'])
                elif key == 'first_name':
                    data_row.append(list[player]['first_name'])
                elif key == 'player_id':
                    data_row.append(list[player]['player_id'])
                    print(data_row)
                    a=data_row.copy()
                    data+=[a,]
                    print(data)
                    data_row.clear()

It is because list is also getting dynamically updated when you clear data_row . So, it is required to make a soft copy which won’t be changed with changes in data_row.

Hope it helps✌️

Answered By: Tushar Bansal

Try:

lst = [
    {"Objet": "premier", "ne pas ": "supprimer", "don't": "delete"},
    {
        "name": "Bridoux",
        "first_name": "Justin",
        "birthday": "14/05/2000",
        "note": "aucune",
        "player_id": 1,
        "total_score": 0,
        "tournament_score": 0,
    },
    {
        "name": "Clerico",
        "first_name": "Leonard",
        "birthday": "07/08/1969",
        "note": "aucune",
        "player_id": 2,
        "total_score": 0,
        "tournament_score": 0,
    },
    {
        "name": "Boutroux",
        "first_name": "Yvon",
        "birthday": "29/04/1984",
        "note": "aucune",
        "player_id": 3,
        "total_score": 0,
        "tournament_score": 0,
    },
    {
        "name": "Badeaux",
        "first_name": "Georges",
        "birthday": "17/02/1962",
        "note": "aucune",
        "player_id": 4,
        "total_score": 0,
        "tournament_score": 0,
    },
    {
        "name": "Lajoie",
        "first_name": "Etienne",
        "birthday": "16/06/1978",
        "note": "aucune",
        "player_id": 5,
        "total_score": 0,
        "tournament_score": 0,
    },
    {
        "name": "Foucher",
        "first_name": "Vincent",
        "birthday": "08/01/1990",
        "note": "aucune",
        "player_id": 6,
        "total_score": 0,
        "tournament_score": 0,
    },
    {
        "name": "Coordonnier",
        "first_name": "Pierre",
        "birthday": "20/10/1992",
        "note": "aucune",
        "player_id": 7,
        "total_score": 0,
        "tournament_score": 0,
    },
    {
        "name": "Chaucard",
        "first_name": "Alex",
        "birthday": "10/07/2001",
        "note": "aucune",
        "player_id": 8,
        "total_score": 0,
        "tournament_score": 0,
    },
    {
        "name": "Marchand",
        "first_name": "Enzo",
        "birthday": "06/06/1995",
        "note": "aucune",
        "player_id": 9,
        "total_score": 0,
        "tournament_score": 0,
    },
    {
        "name": "Flantier",
        "first_name": "Noel",
        "birthday": "25/12/1960",
        "note": "OSS 117",
        "player_id": 10,
        "total_score": 0,
        "tournament_score": 0,
    },
]

data = [["Nom", "Prénom", "ID Joueur"]]

for d in lst[1:]:
    data.append([d["name"], d["first_name"], d["player_id"]])

print(data)

Prints:

[
    ["Nom", "Prénom", "ID Joueur"],
    ["Bridoux", "Justin", 1],
    ["Clerico", "Leonard", 2],
    ["Boutroux", "Yvon", 3],
    ["Badeaux", "Georges", 4],
    ["Lajoie", "Etienne", 5],
    ["Foucher", "Vincent", 6],
    ["Coordonnier", "Pierre", 7],
    ["Chaucard", "Alex", 8],
    ["Marchand", "Enzo", 9],
    ["Flantier", "Noel", 10],
]
Answered By: Andrej Kesely