Convert a comma-separated string to a Dictionary in Python

avatar

Borislav Hadzhiev

Last updated: Aug 31, 2022

banner

Photo from Unsplash

Convert a comma-separated string to a Dictionary in Python #

To convert a comma-separated string to a dictionary:

  1. Use the str.split() method to split the string on each comma.
  2. Split each string in the list on the delimiter between the key-value pairs.
  3. Pass the results to the dict() class.
main.py
my_str = "name=Alice,country=Austria,language=German" # ✅ convert comma-separated string to dictionary (dict()) my_dict = dict(item.split('=') for item in my_str.split(',')) # 👇️ {'name': 'Alice', 'country': 'Austria', 'language': 'German'} print(my_dict) # ----------------------------------------------- # ✅ convert comma-separated string to dictionary (dict comprehension) my_dict = {key: value for key, value in [item.split( '=') for item in my_str.split(',')]} # 👇️ {'name': 'Alice', 'country': 'Austria', 'language': 'German'} print(my_dict)

The first example uses the dict() class to convert a comma-separated string to a dictionary.

The first step is to split the string on each comma, semi-colon or any other character that separates the key-value pairs.

main.py
my_str = "name=Alice,country=Austria,language=German" # 👇️ ['name=Alice', 'country=Austria', 'language=German'] print(my_str.split(','))

The next step is to use a generator expression to iterate over the list and split each string on the character that separates the keys and values.

main.py
my_str = "name=Alice,country=Austria,language=German" my_dict = dict(item.split('=') for item in my_str.split(',')) # 👇️ {'name': 'Alice', 'country': 'Austria', 'language': 'German'} print(my_dict)
Generator expressions are used to perform some operation for every element or select a subset of elements that meet a condition.

The str.split() method splits the string into a list of substrings using a delimiter.

The method takes the following 2 parameters:

NameDescription
separatorSplit the string into substrings on each occurrence of the separator
maxsplitAt most maxsplit splits are done (optional)

If the keys and values in your string are separated by colons, make sure to update the separator in the call to the str.split() method.

main.py
my_str = "name:Alice,country:Austria,language:German" my_dict = dict(item.split(':') for item in my_str.split(',')) # 👇️ {'name': 'Alice', 'country': 'Austria', 'language': 'German'} print(my_dict)

Alternatively, you can use a dict comprehension.

main.py
my_str = "name=Alice,country=Austria,language=German" my_dict = {key: value for key, value in [item.split( '=') for item in my_str.split(',')]} # 👇️ {'name': 'Alice', 'country': 'Austria', 'language': 'German'} print(my_dict)

Dict comprehensions are very similar to list comprehensions.

They perform some operation for every key-value pair in the dictionary or select a subset of key-value pairs that meet a condition.

If the values in your dictionary are numbers, use the int() or float() classes to convert them.

main.py
my_str = "x=0.95,y=4.55" my_dict = {key: float(value) for key, value in [item.split( '=') for item in my_str.split(',')]} # 👇️ {'x': 0.95, 'y': 4.55} print(my_dict)

We used the float() class to convert each value to a floating-point number.

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.