Search code examples
pythonpython-3.xdictionarydictionary-comprehension

Convert dictionary values to be inside a list


I´m trying to convert the values inside a dictionary to be in a list (for future upload in mySQL).

So currently I have the dictionary like below:

old_dict = {'key1':'value1','key2':value2,'key3':'value3'}

And I´m looking for something like:

new_dict = {'key1':['value1'],'key2':[value2],'key3':['value3']}

Solution

  • new_dict = {k: [v] for k, v in old_dict.items()}