How to Convert String to Dictionary in Python

Here are 3 ways to convert a string to a dictionary in Python:

(1) ast.literal_eval(my_string) to convert a string that looks like a dictionary to an actual dictionary:

import ast

my_string = '{1: "blue", 2: "green", 3: "red", 4: "yellow", 5: "purple"}'

my_dict = ast.literal_eval(my_string)
print(my_dict)

The result is a dictionary:

{1: 'blue', 2: 'green', 3: 'red', 4: 'yellow', 5: 'purple'}

Examples of different variations of strings (inside a list) converted into dictionaries using ast.literal_eval:

import ast

list_of_strings = ['{1: "blue", 2: "green", 3: "red", 4: "yellow", 5: "purple"}',
                   '{"1": "blue", "2": "green", "3": "red", "4": "yellow", "5": "purple"}',
                   "{1: 'blue', 2: 'green', 3: 'red', 4: 'yellow', 5: 'purple'}",
                   "{'1': 'blue', '2': 'green', '3': 'red', '4': 'yellow', '5': 'purple'}",
                   '{"blue": 1, "green": 2, "red": 3, "yellow": 4, "purple": 5}',
                   '{"blue": "1", "green": "2", "red": "3", "yellow": "4", "purple": "5"}',
                   "{'blue': 1, 'green': 2, 'red': 3, 'yellow': 4, 'purple': 5}",
                   "{'blue': '1', 'green': '2', 'red': '3', 'yellow': '4', 'purple': '5'}"
                   ]

for i in list_of_strings:
    my_dict = ast.literal_eval(i)
    print(my_dict)

Here are the results:

{1: 'blue', 2: 'green', 3: 'red', 4: 'yellow', 5: 'purple'}
{'1': 'blue', '2': 'green', '3': 'red', '4': 'yellow', '5': 'purple'}
{1: 'blue', 2: 'green', 3: 'red', 4: 'yellow', 5: 'purple'}
{'1': 'blue', '2': 'green', '3': 'red', '4': 'yellow', '5': 'purple'}
{'blue': 1, 'green': 2, 'red': 3, 'yellow': 4, 'purple': 5}
{'blue': '1', 'green': '2', 'red': '3', 'yellow': '4', 'purple': '5'}
{'blue': 1, 'green': 2, 'red': 3, 'yellow': 4, 'purple': 5}
{'blue': '1', 'green': '2', 'red': '3', 'yellow': '4', 'purple': '5'}

(2) json.loads(my_string) only if all the keys are surrounded by double quotes:

import json

my_string = '{"1": "blue", "2": "green", "3": "red", "4": "yellow", "5": "purple"}'

my_dict = json.loads(my_string)
print(my_dict)

The result is a dictionary:

{'1': 'blue', '2': 'green', '3': 'red', '4': 'yellow', '5': 'purple'}

Examples of different variations of strings (inside a list) converted into dictionaries using json.loads:

import json

list_of_strings = ['{"1": "blue", "2": "green", "3": "red", "4": "yellow", "5": "purple"}',
                   '{"blue": 1, "green": 2, "red": 3, "yellow": 4, "purple": 5}',
                   '{"blue": "1", "green": "2", "red": "3", "yellow": "4", "purple": "5"}'
                   ]

for i in list_of_strings:
    my_dict = json.loads(i)
    print(my_dict)

The results:

{'1': 'blue', '2': 'green', '3': 'red', '4': 'yellow', '5': 'purple'}
{'blue': 1, 'green': 2, 'red': 3, 'yellow': 4, 'purple': 5}
{'blue': '1', 'green': '2', 'red': '3', 'yellow': '4', 'purple': '5'}

(3) In case the string does not look like a dictionary:

my_string = "1 blue 2 green 3 red 4 yellow 5 purple"

# Split the string into a list based on spaces
my_list = my_string.split(" ")

# Even index items
keys = my_list[::2]
# Odd index items
values = my_list[1::2]

my_dict = {}

# Convert to Dictionary
for k, v in zip(keys, values):
    my_dict[k] = v

print(my_dict)

You’ll get the following dictionary:

{'1': 'blue', '2': 'green', '3': 'red', '4': 'yellow', '5': 'purple'}

You can get the same results using a Dictionary Comprehension:

my_string = "1 blue 2 green 3 red 4 yellow 5 purple"

my_list = my_string.split(" ")

keys = my_list[::2]
values = my_list[1::2]

my_dict = {k: v for k, v in zip(keys, values)}
print(my_dict)

The result:

{'1': 'blue', '2': 'green', '3': 'red', '4': 'yellow', '5': 'purple'}

Leave a Comment