0

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 DataList inside the function ['1', '2', '3', '4', '5', '6']

This is the main.py DataList list after 1 run [['1', '2', '3', '4', '5', '6']]

This is the CleanedValues ['1', '2', '3', '4', '5', '6', '7']

This is the DataList inside the function [['1', '2', '3', '4', '5', '6', '7'], ['1', '2', '3', '4', '5', '6', '7']]

This is the main.py DataList after the 2 runs [['1', '2', '3', '4', '5', '6', '7']], ['1', '2', '3', '4', '5', '6', '7']]

Expected output:

This is the DataList inside the function [['1', '2', '3', '4', '5', '6']]

This is the CleanedValues ['1', '2', '3', '4', '5', '6']

This is the main.py DataList list after 1 run [['1', '2', '3', '4', '5', '6']]

This is the DataList inside 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 DataList after 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.

Rgiz Tk
  • 11
  • 1
  • please fix the indenation of your Python code, as this is currently messed-up, and add the (abbreviated) contents of the two CSV files, too. – Tomalak Feb 04 '21 at 15:35
  • Thanks @Tomalak, code now indented - I can't share the content of the CSV but the data within CleanedValues would be representative e.g. DataList with 2 lists, one containing 6 values, the other containing 6 of the same value and an additional value – Rgiz Tk Feb 04 '21 at 15:41
  • It's empty because you clear it: `CleanedValues.clear()` – juanpa.arrivillaga Feb 04 '21 at 15:58
  • Where do you define these variables? Scope of variables is important. – Pranav Hosangadi Feb 04 '21 at 15:59
  • @juanpa.arrivillaga thanks juanpa, when I clear CleanedValues, why would that mean that DataList is also cleared? I thought I had to CleanedValues.clear() in order to clear that variable of any data it was given during the first use of function, so I didn't end up with duplication – Rgiz Tk Feb 04 '21 at 16:03
  • @PranavHosangadi CleanedValues and DataLists are lists declared within cleaner.py outside of the function's own scope but within the same file, I import cleaner.py into main and try to access the variable by print(DataList) – Rgiz Tk Feb 04 '21 at 16:05
  • Does this answer your question? [List of lists changes reflected across sublists unexpectedly](https://stackoverflow.com/questions/240178/list-of-lists-changes-reflected-across-sublists-unexpectedly) – Pranav Hosangadi Feb 04 '21 at 16:07
  • Thank you @juanpa.arrivillaga I moved the CleanedValues.clear() to the top of the function and now the variable can be printed – Rgiz Tk Feb 04 '21 at 16:08
  • @PranavHosangadi not really a duplicate, the OP isn't using list repetition – juanpa.arrivillaga Feb 04 '21 at 16:09
  • @RgizTk it's really not clear what you are doing. You really shouldn't be using mutable global state like that. Your functions should *take data as arguments*. – juanpa.arrivillaga Feb 04 '21 at 16:10
  • @RgizTk you keep appending **the same list object**. When you `.clear` it, it will show up empty, and since you have added it multiple times, your `DataList` will have several references to the same empty list. – juanpa.arrivillaga Feb 04 '21 at 16:11
  • @juanpa.arrivillaga ugh I swear I had one that was a duplicate for this in my list of common duplicates. :-| Nevertheless, the reason OP sees an empty list outside the function is the same as that question. – Pranav Hosangadi Feb 04 '21 at 16:12
  • Hi @RgizTk, and welcome to Stack Overflow! Please don't edit the answer into the question. Instead, post an answer. – Red Feb 04 '21 at 16:47
  • @RgizTk Why did you undo the change? – Red Feb 04 '21 at 16:58
  • @juanpa.arrivillaga thanks, I've solved my first issue but second issue where the additional "7" appears, as though the second run of the function is overwriting `DataSet[0]` is still present – Rgiz Tk Feb 04 '21 at 16:59
  • @AnnZen apologies, I was making my own changes to show new terminal output for 2nd issue – Rgiz Tk Feb 04 '21 at 17:00
  • @RgizTk I *already explained* you keep appending **the same list**. Moving the `.clear` doesn't actually fix your underlying issue. You shouldn't be using *mutable global state* to begin with. – juanpa.arrivillaga Feb 04 '21 at 17:06
  • @juanpa.arrivillaga I couldn't distinguish what your answer had provided me in terms of my problems...I have had a look at mutable global state and I have created a local list variable for CleanedValues that appends to the global DataLists and the output is as expected - thanks. – Rgiz Tk Feb 04 '21 at 17:16
  • @RgizTk well, you shouldn't be appending to a list using a global variable either, but that will at least fix your underlying issue – juanpa.arrivillaga Feb 04 '21 at 17:20

0 Answers0