I'm new to Python and coding in general (so the following code wont be the cleanest) but coming across the following issue that I can't seem to crack:
Problem
I have 2 CSVs which I'm converting to dictionaries and passing to a function one after another, in order to take the dictionary keys and append them to a list outside of the function, I'm having an issue with:
1 - When printing DataList within the function, it returns a result - when I call it from main.py, returns empty list
2 - The ultimate aim is to remove duplicates after collating data into one list (DataList). Within the function, when I'm appending ClearedValues to DataList, since my lists from the CSVs contain 1-6 in first & 1-7 in second, I'd expect the output (after the function has been run twice) to be:
This is the required DataList within function
[['1', '2', '3', '4', '5', '6'], ['1', '2', '3', '4', '5', '6', '7']]
Instead, there is an additional "7" within DataList[0], see output
cleaner.py:
def DictCleaner(dictionary):
CleanedValues.clear()
for k, v in dictionary.items():
if v != None:
CleanedValues.append(v)
DataList.append(CleanedValues)
print(f"This is the CleanedValues {CleanedValues}”)
print(f"This is the DataList inside the function {DataList}")
main.py
loadCSVs("DataSet1.csv")
print(f"This is the main.py DataList list after 1 run {DataList}")
loadCSVs("DataSet2.csv")
print(f"This is the main.py DataList after the 2 runs {DataList}")
Where CleanedValues and DataLists are lists declared within cleaner.py outside of the function scope
& loadCSVs() is the function that reads the CSV, coverts to a dictionary and returns DictCleaner() with the new dictionary as parameter
Output:
This is the
CleanedValues[['1', '2', '3', '4', '5', '6']]This is the
DataListinside the function['1', '2', '3', '4', '5', '6']This is the main.py
DataListlist after 1 run[['1', '2', '3', '4', '5', '6']]This is the
CleanedValues['1', '2', '3', '4', '5', '6', '7']This is the
DataListinside the function[['1', '2', '3', '4', '5', '6', '7'], ['1', '2', '3', '4', '5', '6', '7']]This is the main.py
DataListafter the 2 runs[['1', '2', '3', '4', '5', '6', '7']], ['1', '2', '3', '4', '5', '6', '7']]
Expected output:
This is the
DataListinside the function[['1', '2', '3', '4', '5', '6']]This is the
CleanedValues['1', '2', '3', '4', '5', '6']This is the main.py
DataListlist after 1 run[['1', '2', '3', '4', '5', '6']]This is the
DataListinside the function[['1', '2', '3', '4', '5', '6'], ['1', '2', '3', '4', '5', '6', '7']]This is the
CleanedValues['1', '2', '3', '4', '5', '6', '7']This is the main.py
DataListafter the 2 runs[['1', '2', '3', '4', '5', '6'], ['1', '2', '3', '4', '5', '6', '7']]
Any suggestions to optimize code or otherwise are greatly appreciated.