Python stripping characters and combine list objects

Viewed 90

I have a massive list that looks similar to this:

['red.color\xa0', '2020-11-27\xa0', 'green.color\xa0', '2020-11-27\xa0', 'blue.color\xa0', '2020-11-27\xa0']
 

I'm trying to figure out how to strip the \xa0 characters out as well as combine every two list items, so it would end up like:

[['red.color', '2020-11-27'], ['green.color', '2020-11-27'], ['blue.color', '2020-11-27']]

I tried this, but failed miserably:

for data in ugly:
    data = data.strip("\xa0")

As for combining every other list item into a new list, well, if I fail at the above you can probably guess I implode at this.

4 Answers

I think you're looking for something like this:


values = ['red.color\xa0', '2020-11-27\xa0', 'green.color\xa0', '2020-11-27\xa0', 'blue.color\xa0', '2020-11-27\xa0']

index = 0
result = []
while index < len(values):
    result.append([values[index].strip("\xa0"), values[index + 1].strip("\xa0")])
    index += 2

print(result) # prints [['red.color', '2020-11-27'], ['green.color', '2020-11-27'], ['blue.color', '2020-11-27']]

Essentially the steps are:

  1. Create a variable called index, that is the index of the first item in the pair
  2. create a variable called result which will be the resulting list
  3. Go into a while loop that iterates through the indexes and appends each pair and strips them
  4. increment the index variable by two, since you added 2 variables to the resulting list

Your approach is correct but when you use for item in list, item is just a temporary variable mutating it won't have any impact on the original list. You should use indices like this.

l = ['red.color\xa0', '2020-11-27\xa0', 'green.color\xa0', '2020-11-27\xa0', 'blue.color\xa0', '2020-11-27\xa0']

for i in range(len(l)):
    l[i] = l[i].strip("\xa0")
l2 = []
for i in range(0,len(l),2):
    l2.append([l[i], l[i+1]])

print(l2) #[['red.color', '2020-11-27'], ['green.color', '2020-11-27'], ['blue.color', '2020-11-27']]

\xa0 is actually non-breaking space. Replace it like below using List Comprehension:

In [3731]: ugly = ['red.color\xa0', '2020-11-27\xa0', 'green.color\xa0', '2020-11-27\xa0', 'blue.color\xa0', '2020-11-27\xa0']
In [3736]: x = [i.replace(u'\xa0', u'') for i in ugly]

Now, combine every 2 elements of list using zip:

In [3741]: y = [list(i) for i in zip(x[0::2], x[1::2])]
In [3742]: y
Out[3742]: 
[['red.color', '2020-11-27'],
 ['green.color', '2020-11-27'],
 ['blue.color', '2020-11-27']]

To delete the '\xa0'part you can be use the string.replace('\xa0',"") method. Then you have to iterate over the list with a step range of 2 and pair the current element with the next one. Your code could look somewhat like this:

list1 = ['red.color\xa0', '2020-11-27\xa0', 'green.color\xa0', '2020-11-27\xa0', 'blue.color\xa0', '2020-11-27\xa0']
list2 = [x.replace("\xa0", "") for x in list1]

new_list = list()

for x in range(0, len(list2), 2):
    new_list.append([list2[x], list2[x+1]])

print(new_list)
Related