Is there a way to convert a variable name 'this' to this?

I have variables a, b, c, e from a python program. I want to construct a dictionary like this

d = {'a': a, 'b': b, 'c': c, 'e':e}

Is there a quicker way to construct such a dictionary given

[a, b, c, e]

or

['a', 'b', 'c', 'e']

Of course, in the real context, the variable names are longer, and the number of variables are higher.

That might be possible. But why do you want to do this? (See XY Problem for why I am asking.)

1 Like

There is always a way, though not every idea is always advised. For instance, the approach below is a bit hack-ish, but without more information about the structure of your program and what your actual needs are, it’s not really possible to suggest much better.

In [1]: a, b, c, e = 10, 20, 30, 50

In [2]: keys = ("a", "b", "c", "e")

In [3]: d = { k: globals().get(k) for k in keys }

In [4]: d
Out[4]: {'a': 10, 'b': 20, 'c': 30, 'e': 50}
1 Like

Thanks. I want to dump a lot of variables (dictionaries, dataframes, etc, which are a, b, c, e in my question) by pickle, but I need to dump to multiple pkl files unless I create a new big dictionary to contain all variables. If I create a new big dictionary, then I just need to create one pickle file

Here’s another way:

keys = ("a", "b", "c", "e")
d = dict(zip(keys, map(eval, keys)))
1 Like

Why can’t you just dump them into one?

What do you mean with “quicker”? Since they’re “dictionaries, dataframes, etc”, I doubt you’ll notice a speed difference between any methods (unless you do something artificially bad).

1 Like

You can call pickle.dump(obj, file) multiple times with the same open file:

with file("mydump.pkl", "wb") as f:
    pickle.dump(a, f)
    pickle.dump(b, f)
    pickle.dump(c, f)
    pickle.dump(d, f) 

But in any case you seem to need to know which objects you are pickling (i.e., their names). So you can just either do the pickling or make the dict by hand.

Is the more general problem that you somehow want to save and restore the state of your program?

You don’t need a dictionary for this. You can define a function to pickle all of them, and then use the method that Andrew described:

def pickle_all(outfile, *args):
    with open(outfile, "w+b") as f:
        for obj in args:
             pickle.dump(obj, f)

Alternatively, you could also define this by pickling them all together, as one big tuple:

def pickle_all(outfile, *args):
    with open(outfile, "w+b") as f:
        pickle.dump(args, f)

Both of those can be called on whatever number of objects you have:

pickle_all(x, y)
pickle_all(a, b, c)  # takes any number of arguments

If for whatever reason you also want to pickle the original variables names of your objects, then you could do so by using **kwargs (keyword arguments) which is a dictionary constructed when you’re calling the function:

def pickle_all(outfile, **kwargs):
     with open(outfile, "w+b") as f:
        pickle.dump(kwargs, outfile)

To be called as

pickle_all(outfile, a=a, b=b, c=c, df=df)  # etc

Or – if you already have some dictionary, you could then also call:

pickle_all(outfile, **my_objects_dict)

If you’re wondering how the magic of **kwargs works, then you can quickly get a sense of this by running:

>>> def fun(**kwargs):
    print(type(kwargs))
    print(kwargs)
    print(kwargs.items())
>>> fun(a=1, b=2, c="test")