How to get total value of everything in a list with "$" on the values?
Question:
I have a list of data from a csv file consisting of [‘-$323’, ‘$8’, ‘-$4’, ‘$384’,………]. I would like to get the total value of everything in the list but as it is a str type and there is a $, i’m unable to convert it to a int or float.
I tried changing the type of the list but it resulted in an error. Is there any way to get the total value in the list?
for price, quantity in cluster1:
price_c1.append(price.replace("$",""))
# price_c1.append(float(price.replace("$","")) results in a ValueError
ValueError: could not convert string to float: '1,996'
Answers:
You can use [int(i.replace("$", "")) for i in l]
to remove the $
Or you can change the numbers to int by using int(str[1:]) for str in list
(This is not perfect, since if the 1st letter is -
it can cut that off, but i think the 1st item of your list is a typo :))
The [1:]
Means slice off the 1st letter from the string, so the $
will be gone and you can convert the str to a int by using int()
amounts = ['-$323', '$8', '$-4', '$384']
# A list to hold our dollar amounts as ints
dollars_as_int = []
for amount in amounts:
# Check to make sure the $ is in the string so we don't
# get a value error.
if "$" in amount:
# Get the index of the dollar sign.
index_of_dollar = amount.index("$")
# Slice it out and append it to another list.
dollars_as_int.append(int(amount[index_of_dollar + 1:]))
# Or another approach:
for amount in amounts:
dollars_as_ints.append(int(amount.replace("$", "")))
for amount in dollars_as_int:
print(amount)
# Output:
323
8
-4
384
I have a list of data from a csv file consisting of [‘-$323’, ‘$8’, ‘-$4’, ‘$384’,………]. I would like to get the total value of everything in the list but as it is a str type and there is a $, i’m unable to convert it to a int or float.
I tried changing the type of the list but it resulted in an error. Is there any way to get the total value in the list?
for price, quantity in cluster1:
price_c1.append(price.replace("$",""))
# price_c1.append(float(price.replace("$","")) results in a ValueError
ValueError: could not convert string to float: '1,996'
You can use [int(i.replace("$", "")) for i in l]
to remove the $
Or you can change the numbers to int by using int(str[1:]) for str in list
(This is not perfect, since if the 1st letter is -
it can cut that off, but i think the 1st item of your list is a typo :))
The [1:]
Means slice off the 1st letter from the string, so the $
will be gone and you can convert the str to a int by using int()
amounts = ['-$323', '$8', '$-4', '$384']
# A list to hold our dollar amounts as ints
dollars_as_int = []
for amount in amounts:
# Check to make sure the $ is in the string so we don't
# get a value error.
if "$" in amount:
# Get the index of the dollar sign.
index_of_dollar = amount.index("$")
# Slice it out and append it to another list.
dollars_as_int.append(int(amount[index_of_dollar + 1:]))
# Or another approach:
for amount in amounts:
dollars_as_ints.append(int(amount.replace("$", "")))
for amount in dollars_as_int:
print(amount)
# Output:
323
8
-4
384