2

As working on a program using Dict in Python, I am getting a format other than required: the code is as follows:

a = {1:2,2:3,3:4,4:8}
print(a.values())

getting an output:

dict_values([2, 3, 4, 8])

here in the output, there is no requirement of "dict_values" and "{ [ ] }", How to remove these wanted things? Actually, I have tried striping but I am thinking of some professional functions to use.

VIKAS RATHEE
  • 97
  • 2
  • 13

2 Answers2

3

You may use the * operator to unpack the sequence and print the values.

a = {1:2,2:3,3:4,4:8}
print(*a.values())
#2 3 4 8

You can also customize the separator and the end using the keyword arguments sep and end respectively in the call of the print function.
For example:

print(*a.values(), sep=',')
abc
  • 11,579
  • 2
  • 26
  • 51
1

you could join the dict_values (which you need to cast to str) to a string:

', '.join(str(x) for x in a.values())
hiro protagonist
  • 44,693
  • 14
  • 86
  • 111