Add columns of data to a list of row dictionaries

I know this question is probably as old as the hills and related questions seem pretty common on Stackflow but I can’t find an answer I saw not very long ago that looked very similar. I don’t want the answer but just a hint.
I am using the csv module and csv.DictReader to load data from 3 columns of a csv file into a tkinter treeview:

ways to shorten the path and filename would be helpful

def load_file
#with open(’/storage/emulated/0/Pydroid/GUI/Python - Import CSV File To Tkinter Table/test.csv’) as f:

reader = csv.DictReader(f, delimiter=’,’)

for row in reader:

num_recordlines +=1

firstname = row[‘firstname’]

lastname = row[‘lastname’]

address = row[‘address’]

tree.insert("", 0, values=(firstname, lastname, address))

This works great!


But what I really want to do is add data for two more columns/fields, and the data for column ‘E’/field4 is a sequential string that would be generated by a generator function if I was more familiar with generators.Basically, it’s just a record number. (e.g. XLT001, XLT002, …) I can add a string to every row/line/record of field3 with:
field3 = ‘some comment’
but the data for field4 changes for every row. I’m not sure it’s a good idea to stick a function or another loop into the csv reader loop. So, if I take the treeview insertion out of the csv reader loop, I get a list of dictionaries for each row, but the insertion code wants a dictionary and I seem to need to slice and maybe enumerate the list and update each row dictionary, maybe with a second for loop. Something like:
for dictionary in enumerate(row_list):
for key in row_list [dictionary[3:5]]:
dictionary.update({key:record_dict[n]} where record_dict[n] is the string value (e.g. ‘XLT001’) from an ordered dictionary based on the number of records loaded in (num_recordlines) or row number for the dictionary being updated. I’m not sure how to use the enumerate number for n.
I don’t want a complete solution, just a hint at the syntax for the comprehension.
The resulting dictionary goes into a for loop that feeds:
tree.insert("", 0, values=(firstname, lastname, address, field3, field4))

When I put my sequential string maker loop in the csv reader loop I got the values in reverse order

The reason the record ids are in descending order is because tree.insert ("", 0, … inserts each row as first CHILD.So each record is added to the top of the list. To get the records in ascending order, use:
tree.insert("", ‘end’, …
I found this out by adding numbers to the left of the names in the CSV file and saw that they were inserted in descending order.

Similar question? I have used row [0], row.append, firstnames.append(firstname), names[i] etc. to collect data into a list for writer.writerows (results) but I don’t think I want a list here yet. Maybe treeview will accept a list in a different form of ‘insert’.I don’t know.
result = {}
for row in reader:
key = row[0] # the fieldname
result[key] = row [1:]
result = {row[0]:row [1:] for row in reader}

It’s been a while since I used python, but if I recall, when you enumerate a list in a for loop , the first result is the index of the list item and the second is the item itself. example:

myList= ["free", "Code", "Camp"]

for idx, listItem in enumerate(myList):
  print idx, listItem

would result in

0 free
1 Code
2 Camp

If you want to manually set the start of the index from say 2, then enumerate provides for an additional argument like so

myList= ["free", "Code", "Camp"]

for idx, listItem in enumerate(myList,2):
  print idx, listItem

2 free
3 Code
4 Camp

I think you can use these indices to achieve what you want, but without looking further into your code I am not sure

Thank you for that information and snippet regarding enumerate. It has led me to nested dictionaries and it looks promising. I should have the answer very soon as now I can pull keys and values from the nested dictionaries and add a nested dictionary.
my_dict = {}
‘’‘Simulate 2 rows read in by csv.DictReader.’’’
row_list = [{‘firstname’:‘John’, ‘lastname’:‘Smith’, ‘address’:‘London’}, {‘firstname’:‘Jane’, ‘lastname’:‘Doe’, ‘address’:‘Paris’} ]
print(row_list)

for idx, listItem in enumerate(row_list,101):

print(idx, listItem)

my_dict[idx] = listItem
print("my_dict: {}".format(my_dict))

for p_id, p_info in my_dict.items():
print("\nPerson ID:", p_id)

for key in p_info:
    print(key + ':', p_info[key])