Convert List Of Dictionary into Python String
Last Updated :
09 Feb, 2024
Working with data structures like lists of dictionaries is a common task in Python programming. Sometimes, you may need to convert a list of dictionaries into a string for various reasons, such as storing it in a file, sending it over the network, or displaying it in a specific format. In this article, we will explore three simple and commonly used methods for converting a list of dictionaries into a string in Python.
How To Convert List Of Dictionary Into String?
Below, are the methods for How To Convert List Of Dictionary Into String in Python.
Convert List Of Dictionary Into String Using str()
Function
In the code, a sample list of dictionaries, `list_of_dicts`, is created with information about individuals. The variable `result_string` is assigned the string representation of `list_of_dicts` using `str()`, and its type is printed along with the original list’s type.
Python3
list_of_dicts = [{ 'name' : 'John' , 'age' : 25 }, { 'name' : 'Alice' , 'age' : 30 }]
result_string = str (list_of_dicts)
print ( type (list_of_dicts))
print (result_string)
print ( type (result_string))
|
Output
<class 'list'>
[{'name': 'John', 'age': 25}, {'name': 'Alice', 'age': 30}]
<class 'str'>
Convert List Of Dictionary Into String Using For Loop
In the example, below code, a sample list of dictionaries, list_of_dicts
, is given. Method 1 uses a loop to concatenate key-value pairs from each dictionary into a string, removing the trailing comma. The resulting string is printed along with the types of the original list and the final string.
Python3
list_of_dicts = [{ 'name' : 'John' , 'age' : 25 }, { 'name' : 'Alice' , 'age' : 30 }]
result_string = ''
for dictionary in list_of_dicts:
for key, value in dictionary.items():
result_string + = f "{key}: {value}, "
result_string = result_string.rstrip( ', ' )
print ( type (list_of_dicts))
print (result_string)
print ( type (result_string))
|
Output
<class 'list'>
name: John, age: 25, name: Alice, age: 30
<class 'str'>
Convert List Of Dictionary Into String Using List Comprehension
In this example code, a sample list of dictionaries, list_of_dicts
, is provided. Method 2 utilizes list comprehension to format key-value pairs from each dictionary into a list of strings, which are then joined to create the final string.
Python3
list_of_dicts = [{ 'name' : 'John' , 'age' : 25 }, { 'name' : 'Alice' , 'age' : 30 }]
result_list = [ ', ' .join([f "{key}: {value}" for key, value in dictionary.items()]) for dictionary in list_of_dicts]
result_string = ', ' .join(result_list)
print ( type (list_of_dicts))
print (result_string)
print ( type (result_string))
|
Output
<class 'list'>
name: John, age: 25, name: Alice, age: 30
<class 'str'>
Convert List Of Dictionary Into String Using json.dumps
Method
In this example, below code, the json.dumps
method is used to convert the list_of_dicts
(a list of dictionaries) into a formatted JSON string with an indentation of 2 spaces. The resulting JSON string is printed along with the types of the original list and the final string.
Python3
import json
list_of_dicts = [{ 'name' : 'John' , 'age' : 25 }, { 'name' : 'Alice' , 'age' : 30 }]
result_string = json.dumps(list_of_dicts, indent = 2 )
print ( type (list_of_dicts))
print (result_string)
print ( type (result_string))
|
Output
<class 'list'>
[
{
"name": "John",
"age": 25
},
{
"name": "Alice",
"age": 30
}
]
<class 'str'>
Share your thoughts in the comments
Please Login to comment...