Storing a List as a variable

Erlebacher
Erlebacher Registered Posts: 82 ✭✭

I would like to have a project variable that is a list. How is that done? To be clear, I'd like a list such as:

"list": "['a','b','c']"

I thought that this would work given that Json interprets this as a strong. I get an error:

Error in Python process: At line 84: <class 'KeyError'>: ['asd', 'def']

I have not found a combination that worked. My Python code is:

vars = Dataiku.get_custom_variables()
print("keep_cols: ", keep_cols)

Thanks for any insights.


Operating system used: Mac, Ventura

Tagged:

Best Answer

  • Zach
    Zach Dataiker, Dataiku DSS Core Designer, Dataiku DSS Adv Designer, Registered Posts: 153 Dataiker
    edited July 17 Answer ✓

    Hi @Erlebacher
    ,

    You can load a project variable that's a list in Python by using json.loads()

    For example, if I have a project variable that looks like this:

    {
      "keep_cols": ["a", "b", "c"]
    }

    I can load it using the following code:

    import json
    import dataiku
    
    vars = dataiku.get_custom_variables()
    keep_cols = json.loads(vars["keep_cols"])
    print("keep_cols: ", keep_cols)

    Thanks,

    Zach

Setup Info
    Tags
      Help me…