Search code examples
pythonweb-scrapingstrip

Remove a substr from string items in a list


list = [ 'u'adc', 'u'toto', 'u'tomato', ...]

What I want is to end up with a list of the kind: list2 = [ 'adc', 'toto', 'tomato'... ]

Can you please tell me how to do that without using regex? I'm trying:

for item in list:
            list.extend(str(item).replace("u'",''))
            list.remove(item)

but this ends up giving something of the form [ 'a', 'd', 'd', 'm'...]

In the list I may have an arbitrary number of strings.


Solution

  • you can encode it to "utf-8" like this:

    list_a=[ u'adc', u'toto', u'tomato']
    list_b=list()
    for i in list_a:
        list_b.append(i.encode("utf-8"))
    list_b
    

    output:

    ['adc', 'toto', 'tomato']
    

    Or you can use str function:

    list_c = list()
    for i in list_a:
        list_c.append(str(i))
    list_c
    

    Output:

    ['adc', 'toto', 'tomato']