Storing a List as a variable

Solved!
Erlebacher
Level 4
Storing a List as a variable

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

0 Kudos
1 Solution
ZachM
Dataiker

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

View solution in original post

0 Kudos
1 Reply
ZachM
Dataiker

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

0 Kudos