Instead of manually formatting the values in your list to strings containing currency representations, it's a good idea to use standard library functions that are dedicated to exactly this job. Here, what you may want to use are the functions from the locale
module. These functions provide a convenient way for instance to represent dates, times, and currencies in a format that is appropriate for the current locale settings. The locale can be either set by the operating system of the computer the program runs on, or by the programmer of the program itself.
The first thing you have to do is load the locale
module. Then you set up the locale setting, either to the system default:
import locale
locale.setlocale(locale.LC_ALL, "") # to use the system-wide locale setting
or to a locale of your own choosing:
import locale
locale.setlocale(locale.LC_ALL, "en_US.utf-8") # to use the USA locale
Now, you can use the currency
function to format a value using the current locale setting:
lst = [200, 4002, 4555, 7533]
cur_lst = [locale.currency(val) for val in lst]
print(cur_list)
['$200.00', '$4002.00', '$4555.00', '$7533.00']
The currency
function has three options that allow you to tweak the output. The grouping
option will insert appropriate grouping marks to separate thousands, millions etc.:
locale.currency(123456, grouping=True)
'$123,456.00'
locale.currency(123456, grouping=False) # the default
'$123456.00'
The international
option uses the international standard currency abbreviation instead of the currency symbol:
locale.currency(123456, international=True)
'USD 123456.00'
locale.currency(123456, international=False) # the default
'$123456.00'
Lastly, setting the symbol
option to True
suppresses showing the currency symbol altogether.