Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

my list of dictionaries is not correctly filled

I’m trying to work with a list of dictionaries and I have a problem.

Here is my code:

for i in range(len(self.dict_trie_csv_infos)):
            self.template_json.append(self.template_json[0])
            #add values to json

for i in range(len(self.dict_trie_csv_infos)):
        self.template_json[i]['id'] = self.dict_trie_csv_infos[i]['id']
        print(self.template_json[i]['id'])
        self.template_json[i]['name'] = self.dict_trie_csv_infos[i]['name']
        self.template_json[i]['title'] = self.dict_trie_csv_infos[i]['event title']
        self.template_json[i]['startDateTime'] = self.dict_trie_csv_infos[i]

        #fill the smart contract now
        self.template_json[i]['ticketCollection'][0]['collectionName'] = self.dict_trie_csv_infos[i]['smart_contract']['collectionName']
        self.template_json[i]['ticketCollection'][0]['endTime'] = self.dict_trie_csv_infos[i]['smart_contract']['sale_params']['end_time']                
        self.template_json[i]['ticketCollection'][0]['pricePerToken'] = self.dict_trie_csv_infos[i]['smart_contract']['sale_params']['price_per_token']
        self.template_json[i]['ticketCollection'][0]['totalTicketsCount'] = ''             
        self.template_json[i]['ticketCollection'][0]['soldTicketsCount'] = ''

print(self.template_json[0])

The first print prints 1 then 2, then 3… but self.template_json[0] prints my last element
and print(self.template_json) it gives me the same result every time.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

{'id': '4', 'name': 'Stade de France', 'title': 'Coldplay World Tour', 'startDateTime': 1651615200, 'endDateTime': 1659045600, 'address': '', 'locationName': '93200 Saint-Denis', 'totalTicketsCount': '10000', 'assetUrl': 'https://coldplay.com/coldplay_asset.mp4', 'lineup': [''], 'ticketCollection': [{'collectionName': 'KT2jH58CPkYBe3bRuTCET6A4NhnosX2BAnp9', 'scAddress': '', 'collectionAddress': '', 'pricePerToken': 50, 'maxMintPerUser': '', 'saleSize': '', 'endTime': 1659045600, 'totalTicketsCount': '', 'soldTicketsCount': ''}], 'adress': 'KT1ffDxCJH9EPimNm19ifBEgG9bFRgptJwop'}

there is my template_json before the loop:

[{'id': '', 'name': '', 'title': '', 'startDateTime': '', 'endDateTime': '', 'address': '', 'locationName': '', 'totalTicketsCount': '', 'assetUrl': '', 'lineup': [], 'ticketCollection': [{'collectionName': '', 'scAddress': '', 'collectionAddress': '', 'pricePerToken': '', 'maxMintPerUser': '', 'saleSize': ''}]}, {'id': '', 'name': '', 'title': 
'', 'startDateTime': '', 'endDateTime': '', 'address': '', 'locationName': '', 'totalTicketsCount': '', 'assetUrl': '', 'lineup': [], 'ticketCollection': [{'collectionName': '', 'scAddress': '', 'collectionAddress': '', 'pricePerToken': '', 'maxMintPerUser': '', 'saleSize': ''}]}, {'id': '', 'name': '', 'title': '', 'startDateTime': '', 'endDateTime': '', 'address': '', 'locationName': '', 'totalTicketsCount': '', 'assetUrl': '', 'lineup': [], 'ticketCollection': [{'collectionName': '', 'scAddress': '', 'collectionAddress': '', 'pricePerToken': '', 'maxMintPerUser': '', 'saleSize': ''}]}, {'id': '', 'name': '', 'title': '', 'startDateTime': '', 'endDateTime': '', 'address': '', 'locationName': '', 'totalTicketsCount': '', 'assetUrl': '', 'lineup': [], 'ticketCollection': [{'collectionName': '', 'scAddress': '', 'collectionAddress': '', 'pricePerToken': '', 'maxMintPerUser': '', 'saleSize': ''}]}, {'id': '', 'name': '', 'title': '', 'startDateTime': '', 'endDateTime': '', 'address': '', 'locationName': '', 'totalTicketsCount': '', 'assetUrl': '', 'lineup': [], 'ticketCollection': [{'collectionName': '', 'scAddress': '', 'collectionAddress': '', 'pricePerToken': '', 'maxMintPerUser': '', 'saleSize': ''}]}, {'id': '', 'name': '', 'title': '', 'startDateTime': '', 'endDateTime': '', 'address': '', 'locationName': '', 'totalTicketsCount': '', 'assetUrl': '', 'lineup': [], 'ticketCollection': [{'collectionName': '', 'scAddress': '', 'collectionAddress': '', 'pricePerToken': '', 'maxMintPerUser': '', 'saleSize': ''}]}]

and there is my "print(self.dict_tries_csv):

{'event_id': 1, 'collection_name': 'Mouse On', 'smart_contract': {'multisig': 'KT1Aer6TxNwoMJejoqsNP8TEN7J6STgMtJcA', 'sale_params': {'is_presale': False, 'metadata_list': [], 'price_per_token': 4, 'max_mint_per_user': 5, 'sale_size': 500, 'sale_currency': {'xtz': None}, 'start_time': 1656626400, 'end_time': 1657490400}, 'collectionName': 'KT1Apf8CPkYBe3bRuTCET6A4NhnosX2BAnp9', 'scAddress': 'KT1AKqxCJH9EPimNm1wo1BEgG9bFRgptJwkk'}, 'id': '1', 'event title': 'Mouse Party', 'event start date': '10/07/2022 18:30', 'event end date': '11/07/2022 01:00', 'name': "L'Astrolabe", 'address of the location': '1 Rue Alexandre Avisse 45000 Orléans', 'total ticket number': '500', 'maximum tickets per user': '5', 'sale start date': '01/07/2022', 'line up': 'Mehdi Maïzi-Rad Cartier-Squidji', 'asset url': 'https://photos.com/mouseparty.png', 'lineup': ['Mehdi Maïzi', 'Rad Cartier', 'Squidji']}

I expected to get id == 1

>Solution :

All your list elements are the same dictionary. You need to copy them.

from copy import deepcopy

for i in range(len(self.dict_trie_csv_infos)):
    self.template_json.append(deepcopy(self.template_json[0]))
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading