Convert Dictionary to String List in Python
Last Updated :
28 Jan, 2025
The task of converting a dictionary to a string list in Python involves transforming the key-value pairs of the dictionary into a formatted string and storing those strings in a list.
For example, consider a dictionary d = {1: 'Mercedes', 2: 'Audi', 3: 'Porsche', 4: 'Lambo'}. Converting this to a string list would result in a list like ['1: Mercedes', '2: Audi', '3: Porsche', '4: Lambo'].
Using list comprehension
List comprehension is a efficient way to convert a dictionary into a list of formatted strings. It combines iteration and string formatting in a single line, making it a highly Pythonic solution. This method is simple to read and write, making it ideal for quick transformations.
Python
d = {1: 'Mercedes', 2: 'Audi', 3: 'Porsche', 4: 'Lambo'}
res = [f"{key}: {val}" for key, val in d.items()]
print(res)
Output['1: Mercedes', '2: Audi', '3: Porsche', '4: Lambo']
Explanation:
- d.items() retrieves all key-value pairs as tuples e.g., (1, 'Mercedes') .
- List comprehension iterates through key-value pairs, formatting each as f"{k}: {v}".
Using map()
map() applies a given transformation function to each element of an iterable. When working with dictionaries, it allows us to process each key-value pair systematically, converting them into formatted strings. This method is particularly useful if we prefer a functional programming approach over explicit loops .
Python
d = {1: 'Mercedes', 2: 'Audi', 3: 'Porsche', 4: 'Lambo'}
res = list(map(lambda kv: f"{kv[0]}: {kv[1]}", d.items()))
print(res)
Output['1: Mercedes', '2: Audi', '3: Porsche', '4: Lambo']
Explanation:
- d.items() retrieves key-value pairs as tuples e.g., (1, 'Mercedes').
- lambda kv: f"{kv[0]}: {kv[1]}" formats each tuple as a string e.g., (1, 'Mercedes') → "1: Mercedes" .
- map() applies the lambda function to each tuple in d.items().
- list() converts the result from map() into a list .
Using join()
join()
combine multiple strings into a single string. When paired with a generator expression, it becomes an elegant solution for transforming and formatting dictionary data into a list of strings. This approach is particularly efficient for tasks where we also need the data as a single string at some point.
Python
d = {1: 'Mercedes', 2: 'Audi', 3: 'Porsche', 4: 'Lambo'}
res= ", ".join(f"{k}: {v}" for k, v in d.items()).split(", ")
print(res)
Output['1: Mercedes', '2: Audi', '3: Porsche', '4: Lambo']
Explanation:
- f"{key}: {val}" for key, val in d.items() formats each tuple as a string e.g., (1, 'Mercedes') → "1: Mercedes".
- ", ".join() combines the formatted strings into a single string, separated by commas .
- .split(", ") splits the combined string back into a list using the comma and space separator.
Using loop
Loop is the traditional approach to convert a dictionary into a string list. It is ideal when we need more control over the transformation process or when additional operations need to be performed alongside formatting .
Python
d = {1: 'Mercedes', 2: 'Audi', 3: 'Porsche', 4: 'Lambo'}
res = [] # initializes empty list
for key, val in d.items(): # iterates over key-value pairs in `d`
res.append(f"{key}: {val}")
print(res)
Output['1: Mercedes', '2: Audi', '3: Porsche', '4: Lambo']
Explanation:
- d.items() retrieves key-value pairs as tuples .
- res.append(f"{key}: {val}") formats each tuple as a string and appends it to the list res.
Similar Reads
Convert List Of Dictionary into String - Python In Python, lists can contain multiple dictionaries, each holding key-value pairs. Sometimes, we need to convert a list of dictionaries into a single string. For example, given a list of dictionaries [{âaâ: 1, âbâ: 2}, {âcâ: 3, âdâ: 4}], we may want to convert it into a string that combines the conte
3 min read
Convert String Float to Float List in Python We are given a string float we need to convert that to float of list. For example, s = '1.23 4.56 7.89' we are given a list a we need to convert this to float list so that resultant output should be [1.23, 4.56, 7.89].Using split() and map()By using split() on a string containing float numbers, we c
2 min read
Python | Convert string enclosed list to list Given a list enclosed within a string (or quotes), write a Python program to convert the given string to list type. Examples: Input : "[0, 2, 9, 4, 8]" Output : [0, 2, 9, 4, 8] Input : "['x', 'y', 'z']" Output : ['x', 'y', 'z'] Approach #1: Python eval() The eval() method parses the expression passe
5 min read
Converting String Content to Dictionary - Python In Python, a common requirement is to convert string content into a dictionary. For example, consider the string "{'a': 1, 'b': 2, 'c': 3}". The task is to convert this string into a dictionary like {'a': 1, 'b': 2, 'c': 3}. This article demonstrates several ways to achieve this in Python.Using ast.
2 min read
Python | Convert List of lists to list of Strings Interconversion of data is very popular nowadays and has many applications. In this scenario, we can have a problem in which we need to convert a list of lists, i.e matrix into list of strings. Let's discuss certain ways in which this task can be performed. Method #1 : Using list comprehension + joi
4 min read
Python - List of float to string conversion When working with lists of floats in Python, we may often need to convert the elements of the list from float to string format. For example, if we have a list of floating-point numbers like [1.23, 4.56, 7.89], converting them to strings allows us to perform string-specific operations or output them
3 min read