Convert a dictionary to a comma-separated string in Python

avatar

Borislav Hadzhiev

Last updated: Aug 31, 2022

banner

Photo from Unsplash

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

To convert a dictionary to a comma-separated string:

  1. Use the str.keys() or str.values() method to get a view of the dictionary's keys or values.
  2. Use the str.join() method to join the keys or values with a comma separator.
main.py
my_dict = {'name': 'Alice', 'age': 30, 'salary': 100} # ✅ join dictionary keys with a comma separator keys_str = ','.join(my_dict.keys()) print(keys_str) # 👉️ 'name,age,salary' # ------------------------------------------- # ✅ join dictionary values with a comma separator values_str = ','.join(str(value) for value in my_dict.values()) print(values_str) # 👉️ 'Alice,30,100' # ------------------------------------------- # ✅ join dictionary items with a comma separator dict_str = ", ".join("=".join([key, str(value)]) for key, value in my_dict.items()) print(dict_str) # 👉️ name=Alice, age=30, salary=100

The first example uses the dict.keys() method to get a view of the dictionary's keys.

main.py
my_dict = {'name': 'Alice', 'age': 30, 'salary': 100} print(my_dict.keys()) # 👉️ dict_keys(['name', 'age', 'salary']) print(my_dict.values()) # 👉️ dict_values(['Alice', 30, 100])

The dict.keys method returns a new view of the dictionary's keys.

The dict.values method returns a new view of the dictionary's values.

The last step is to use the str.join() method to join the dictionary's keys or values into a string with a comma separator.

main.py
my_dict = {'name': 'Alice', 'age': 30, 'salary': 100} keys_str = ','.join(my_dict.keys()) print(keys_str) # 👉️ 'name,age,salary' values_str = ','.join(str(value) for value in my_dict.values()) print(values_str) # 👉️ 'Alice,30,100'

The str.join method takes an iterable as an argument and returns a string which is the concatenation of the strings in the iterable.

Note that the method raises a TypeError if there are any non-string values in the iterable.

If your iterable contains numbers or other types, convert all of the values to string before calling join().

The string the method is called on is used as the separator between the elements.

We used a comma in the examples. If you need to separate the values with a comma followed by a space, make sure to adjust the string the method is called on.

main.py
my_dict = {'name': 'Alice', 'age': 30, 'salary': 100} keys_str = ', '.join(my_dict.keys()) print(keys_str) # 👉️ 'name, age, salary' values_str = ', '.join(str(value) for value in my_dict.values()) print(values_str) # 👉️ 'Alice, 30, 100'

If you need to convert a dictionary to a comma-separated string containing the dictionary's keys and values, use the dict.items() method.

main.py
my_dict = {'name': 'Alice', 'age': 30, 'salary': 100} dict_str = ", ".join("=".join([key, str(value)]) for key, value in my_dict.items()) print(dict_str) # 👉️ name=Alice, age=30, salary=100

The dict.items method returns a new view of the dictionary's items ((key, value) pairs).

main.py
my_dict = {'name': 'Alice', 'age': 30, 'salary': 100} # 👇️ dict_items([('name', 'Alice'), ('age', 30), ('salary', 100)]) print(my_dict.items())

We used the join() method to join each key-value pair with an equal sign separator and then used the join() method again to join the key-value pairs with a comma-separator.

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.