1

I have a list:

['6.26%', '5.94%', '7.47%', '6.90%', '5.99%', '7.94%', '8.75%', 
 '9.01%', '10.07%', '5.51%', '4.87%', '5.88%', '4.26%', '2.97%', 
 '6.38%', '4.93%', '3.96%', '4.62%', '3.73%', '5.15%', '0.86%', 
 '2.68%', '4.01%', '4.89%', '5.84%', '5.23%', '5.57%', '5.53%', 
 '2.39%', '1.00%', '2.44%', '4.65%', '3.66%', '4.60%', '4.54%', 
 '2.30%', '-1.51%', '2.36%', '3.13%', '3.12%', '1.28%', '3.55%', 
 '3.48%', '1.13%', '3.45%']

and I would like to remove all the ' and % from the list to give me a list with integers that I can use to add to another list. I have seen and looked at many questions regarding lists but none seem to apply to my scenario. Help please!

Jab
  • 26,853
  • 21
  • 75
  • 114
  • If you really want to remove the `'`, you should dive deeper into the topics of strings and lists in Python since you have not fully understood them. – Klaus D. Dec 08 '18 at 03:21

2 Answers2

6

Use a list comprehension

values_list = ['6.26%', '5.94%', '7.47%', '6.90%', '5.99%', '7.94%', 
'8.75%', '9.01%', '10.07%', '5.51%', '4.87%', '5.88%', '4.26%', 
'2.97%', '6.38%', '4.93%', '3.96%', '4.62%', '3.73%', '5.15%', 
'0.86%', '2.68%', '4.01%', '4.89%', '5.84%', '5.23%', '5.57%', 
'5.53%', '2.39%', '1.00%', '2.44%', '4.65%', '3.66%', '4.60%', 
'4.54%', '2.30%', '-1.51%', '2.36%', '3.13%', '3.12%', '1.28%', 
'3.55%', '3.48%', '1.13%', '3.45%']

float_list = [float(value.strip('%')) for value in values_list]

This results in:

[6.26, 5.94, 7.47, 6.9, 5.99, 7.94, 8.75, 9.01, 10.07, 5.51, 4.87, 
 5.88, 4.26, 2.97, 6.38, 4.93, 3.96, 4.62, 3.73, 5.15, 0.86,
 2.68, 4.01, 4.89, 5.84, 5.23, 5.57, 5.53, 2.39, 1.0, 2.44, 4.65, 
 3.66, 4.6, 4.54, 2.3, -1.51, 2.36, 3.13, 3.12, 1.28, 3.55, 
 3.48, 1.13, 3.45]
Jab
  • 26,853
  • 21
  • 75
  • 114
  • Worked like a charm, appreciate the quick response and detailed explanation. However I have another question. If my list has nothing to strip yet is in the string format, how would I convert that to a float with nothing to strip? – bambomsmash Dec 08 '18 at 03:40
  • You mean without a %? Just values? – Jab Dec 08 '18 at 03:43
  • Yeah just the values. Like ['5.34', '10.59'] I'm sure it's an easy fix but being a rookie coder has been rough – bambomsmash Dec 08 '18 at 03:47
  • Technically this would still work.. as the strip wouldn't do anything if it doesn't find what it's looking for. Although, if you're just using it for that, then just remove the `strip` altogether. Furthermore if that's the case then [map()](https://docs.python.org/2/library/functions.html#map) would be a *little* more efficient than a list comprehension as described in [this](https://stackoverflow.com/a/1247490/225020) answer. Use map like this: `map(float, myList)` if this is all you're doing. – Jab Dec 08 '18 at 03:53
  • I'm running into all sorts of problems. I'm getting a ValueError: could not convert string to float: '1,560.16' . I have no idea how that would work when that is clearly a string – bambomsmash Dec 08 '18 at 04:41
  • Use the strip method and strip all commas out. It can't strip with commas in there. – Jab Dec 10 '18 at 20:40
2

A simple way (but more inefficient). Remove every % from the string using a for loop. Then you can convert them into floats. For example:

l = ['6.26%', '5.94%',  '7.47%', '6.90%', '5.99%', '7.94%', '8.75%', 
'9.01%', '10.07%', '5.51%', '4.87%', '5.88%', '4.26%', '2.97%', 
'6.38%', '4.93%',  '3.96%', '4.62%', '3.73%', '5.15%', '0.86%', 
'2.68%', '4.01%',  '4.89%', '5.84%', '5.23%', '5.57%', '5.53%', 
'2.39%', '1.00%',  '2.44%', '4.65%', '3.66%', '4.60%', '4.54%', 
'2.30%', '-1.51%', '2.36%', '3.13%', '3.12%', '1.28%', '3.55%', 
'3.48%', '1.13%',  '3.45%']
new = []
for item in l:
    new.append(float(l[:-1]))
l = new

new now equals your wanted list

EDIT: The int() function should actually be float(). This will result in the strings not losing their decimal values.

PYer
  • 458
  • 3
  • 12
  • Why conver them to integers? this would lose their decimal value. Use [float()](https://docs.python.org/2/library/functions.html#float). Also you're only cutting off the last character in the string which isn't advised but it works for this example. Using `.split('%')` or a regex would be better in this regards – Jab Dec 08 '18 at 03:34
  • Sorry. Thanks for warning me about the float(), int() issue. – PYer Dec 08 '18 at 03:45
  • No worries, honest mistake – Jab Dec 08 '18 at 03:45