Again, create a file named transactions.csv
, and populate it with the data provided in the template. This time, however, you are required to add a new field at the beginning of each record. Name it 'number'
, and provide an integer number identifying each record (starting from 0 for the first). The end result should look like this:
0,UK,London,349.00,cash
1,France,Paris,157.00,cash
...
Before you perform any operations on the file, you can iterate over the transactions_data
list and add a new field named number to each dictionary using a counter:
counter = 0
for row in transactions_data:
row['number'] = counter
counter += 1
... or, if you feel like learning more, you can use enumerate()
:
for index, row in enumerate(transactions_data):
row['number'] = index
The enumerate()
function returns a pair (tuple) containing a count (starting from 0) and the values obtained from iterating over a given variable.