I’m trying to get rid of blankspace before and after an element in an array using .strip(), How do i get this to work correctly, I currently have :
JavaScript
x
7
1
ing1 = ['Nivea', 'Water, GlyCerin, Caprylic/Capric Triglyceride, Squalane, Coco-Caprylate/ Caprate']
2
3
ing1list = []
4
for ele in ing1[1:]:
5
ing1list.extend(ele.strip().lower().split(','))
6
print(ing1list)
7
my output which is nearly right, but still has a space before the second element is:
JavaScript
1
2
1
['water', ' glycerin', ' caprylic/capric triglyceride', ' squalane', ' coco-caprylate/ caprate']
2
Advertisement
Answer
I believe you are stripping before splitting. You want to split before stripping.
JavaScript
1
11
11
1
ing1 = ['Nivea', 'Water, GlyCerin, Caprylic/Capric Triglyceride, Squalane, Coco-Caprylate/ Caprate']
2
3
ing1list = []
4
5
for ele in ing1[1:]:
6
sub_ele = ele.split(",")
7
for ele2 in sub_ele:
8
ing1list.append(ele2.strip().lower())
9
10
print(ing1list)
11
Output:
JavaScript
1
2
1
['nivea', 'water', 'glycerin', 'caprylic/capric triglyceride', 'squalane', 'coco-caprylate/ caprate']
2
Also, ing1 could probably be a string.