
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Convert Dictionary Values to Strings in Python
A dictionary in Python is an unordered collection of key-value pairs. It is a data structure that allows you to store and retrieve values based on a unique key associated with each value. Keys in a dictionary are used to uniquely identify values, and they must be immutable, such as strings, numbers, or tuples. On the other hand, the associated values in a dictionary can be of any data type and can be mutable.
When we want to convert a dictionary values to strings, we need to iterate over the dictionary using a loop and convert each value to a string individually using the str() function.
Input Output Scenarios
See the following input-output scenarios to understand the concept of converting the dictionary values to strings.
Input dictionary: {'red': 3, 'yellow': 2, 'green': 3} Output convered dictionary: {'red': '3', 'yellow': '2', 'green': '3'}
The Output convered dictionary contains the original keys and their corresponding values converted to strings.
Input dictionary: {'k1': 12, 'numbers': [1, 2, 3], 'tuple of numbers': (10, 11, 2)} Output convered dictionary: {'k1': '12', 'numbers': '[1, 2, 3]', 'tuple of numbers': '(10, 11, 2)'}
In the converted dictionary, the values integer 12, list [1, 2, 3], and tuple (10, 11, 2) are converted to strings '12', '[1, 2, 3]', and '(10, 11, 2)'.
In this article, we will see different approaches to converting the dictionary values to strings.
Using a for loop
In this approach, we will update the original dictionary with the converted values to strings. We will iterate over the original dictionary using the items() method in a for loop, and for each value, we convert the value to a string using the str() function.
Example
Here's an example, to convert dictionary values to strings using the str() function and a for loop.
# Create the dictionary dictionary = {'red': 3, 'yellow': 2, 'green': 3} print('Input dictionary:', dictionary) for key, value in dictionary.items(): dictionary[key] = str(value) # Display the output print('Output convered dictionary:', dictionary)
Output
Input dictionary: {'red': 3, 'yellow': 2, 'green': 3} Output convered dictionary: {'red': '3', 'yellow': '2', 'green': '3'}
Using the dictionary comprehension
Same as previous approach, here we will create a new dictionary called converted_dict using dictionary comprehension. We will iterate over the original dictionary using the items() method, and for each key-value pair, we convert the value to a string using the str() function. The resulting key-value pair is then added to the converted_dict.
Example
Here's an example, to convert dictionary values to strings using dictionary comprehension.
# Create the dictionary dictionary = {'k1': 123, 'numbers': [1, 2, 3], 'tuple of numbers': (10, 11, 2)} print('Input dictionary:') print(dictionary) converted_dict = {key: str(value) for key, value in dictionary.items()} # Display the output print('Output convered dictionary:') print(converted_dict)
Output
Input dictionary: {'k1': 123, 'numbers': [1, 2, 3], 'tuple of numbers': (10, 11, 2)} Output convered dictionary: {'k1': '123', 'numbers': '[1, 2, 3]', 'tuple of numbers': '(10, 11, 2)'}
Using the join() method
In this approach, we are going to convert the dictionary values into a single string. Here we are using the .join() method to concatenate all the values together into a single string. The expression "".join() specifies an empty string as the separator between the values.
Example
Here is an example of converting the dictionary values into a single string.
# Create the dictionary dictionary = {1:'t', 2:'u', 3:'t', 4:'o', 5:'r', 6:'i', 7:'a', 8:'l', 9:'s', 10:'p', 11:'o', 12:'i', 13:'n', 14:'t'} print('Input dictionary:') print(dictionary) converted_dict = "".join(v for _,v in dictionary.items()) # Display the output print('Output convered string:') print(converted_dict)
Output
Input dictionary: {1: 't', 2: 'u', 3: 't', 4: 'o', 5: 'r', 6: 'i', 7: 'a', 8: 'l', 9: 's', 10: 'p', 11: 'o', 12: 'i', 13: 'n', 14: 't'} Output convered string: tutorialspoint