Last updated: Apr 8, 2024
Reading timeยท4 min
To join the values of a dictionary into a string:
str.join()
method on a string separator.values()
method on the dict and pass the result to the join()
method.my_dict = {'a': 'one', 'b': 'two', 'c': 'three'} values = ' '.join(my_dict.values()) print(values) # ๐๏ธ one two three keys = ' '.join(my_dict) print(keys) # ๐๏ธ a b c items = ' '.join(f'{key} {value}' for key, value in my_dict.items()) print(items) # ๐๏ธ a one b two c three
The str.join() method takes an iterable as an argument and returns a string which is the concatenation of the strings in the iterable.
The string the join()
method is called on is used as the separator between the
elements.
my_dict = {'a': 'one', 'b': 'two', 'c': 'three'} values = ','.join(my_dict.values()) print(values) # ๐๏ธ one,two,three
Note that the method raises a TypeError
if there are any non-string values in
the iterable.
You can use the map()
function to convert each dictionary value to a string
before passing them to the join()
method.
my_dict = {'a': 1, 'b': 2, 'c': 3} values = ' '.join(map(str, my_dict.values())) print(values) # ๐๏ธ 1 2 3
The map() function takes a function and an iterable as arguments and calls the function with each item of the iterable.
The map()
function passes each dictionary value to the
str() class.
Alternatively, you can use a generator expression.
my_dict = {'a': 1, 'b': 2, 'c': 3} values = ' '.join(str(val) for val in my_dict.values()) print(values) # ๐๏ธ 1 2 3
Generator expressions are used to perform some operation for every element or select a subset of elements that meet a condition.
The dict.values() method returns a new view of the dictionary's values.
my_dict = {'id': 1, 'name': 'Bobby'} print(my_dict.values()) # ๐๏ธ dict_values([1, 'Bobby'])
The string the join()
method is called on is used as the separator between the
elements.
my_dict = {'a': 1, 'b': 2, 'c': 3} values = '-'.join(str(val) for val in my_dict.values()) print(values) # ๐๏ธ 1-2-3
If you need to join the dictionary's values with spaces, call the method on a string that contains a space.
my_dict = {'a': 1, 'b': 2, 'c': 3} values = ' '.join(str(val) for val in my_dict.values()) print(values) # ๐๏ธ '1 2 3'
Similarly, you can use a newline (\n
) to join the values in the dict
with a
newline character.
my_dict = {'a': 1, 'b': 2, 'c': 3} values = '\n'.join(str(val) for val in my_dict.values()) # 1 # 2 # 3 print(values)
If you don't need a separator and just want to join the dictionary's values into
a string, call the join()
method on an empty string.
my_dict = {'a': 1, 'b': 2, 'c': 3} values = ''.join(str(val) for val in my_dict.values()) print(values) # ๐๏ธ '123'
If you need to join the keys of a dictionary into a string:
str.join()
method on a string separator.join()
method.my_dict = {'name': 'Bobby', 'country': 'Belgium', 'job': 'accountant'} keys = ', '.join(my_dict) print(keys) # ๐๏ธ 'name, country, job' values = ', '.join(my_dict.values()) print(values) # ๐๏ธ Bobby, Belgium, accountant items = ', '.join(f'{key} {value}' for key, value in my_dict.items()) print(items) # ๐๏ธ name Bobby, country Belgium, job accountant
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 dictionary contains integer keys (or other types), convert them to
strings before calling join()
.
my_dict = {'name': 'Bobby', 'country': 'Belgium', 'job': 'accountant', 100: 200} keys = ', '.join(map(str, my_dict)) print(keys) # ๐๏ธ 'name, country, job, 100' values = ', '.join(map(str, my_dict.values())) print(values) # ๐๏ธ Bobby, Belgium, accountant, 200 items = ', '.join(f'{key} {value}' for key, value in my_dict.items()) print(items) # ๐๏ธ name Bobby, country Belgium, job accountant, 100 200
The map() function takes a function and an iterable as arguments and calls the function with each item of the iterable.
str.join()
method.The string the join()
method is called on is used as the separator between the
elements.
my_dict = {'name': 'Bobby', 'country': 'Belgium', 'job': 'accountant'} keys = '-'.join(my_dict) print(keys) # ๐๏ธ 'name-country-job'
If you need to join the dictionary's keys with spaces, call the method on a string that contains a space.
my_dict = {'name': 'Bobby', 'country': 'Belgium', 'job': 'accountant'} keys = ' '.join(my_dict) print(keys) # ๐๏ธ 'name country job'
Similarly, you can use a newline (\n
) to join the keys in the dict
with a
newline character.
my_dict = {'name': 'Bobby', 'country': 'Belgium', 'job': 'accountant'} keys = '\n'.join(my_dict) # name # country # job print(keys)
If you don't need a separator and just want to join the dictionary's keys into a
string, call the join()
method on an empty string.
my_dict = {'name': 'Bobby', 'country': 'Belgium', 'job': 'accountant'} keys = ''.join(my_dict) print(keys) # ๐๏ธ 'namecountryjob'
You can learn more about the related topics by checking out the following tutorials: