KDX2
KDX2

Reputation: 1033

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.

Upvotes: 1

Views: 102

Answers (6)

Keerthana Prabhakaran
Keerthana Prabhakaran

Reputation: 3787

Your input is nothing but a json! You the dump each item in the list(which is a json!) to get the desired output!

Since your output comes with quotes - you need to strip(beginning and trailing) them!

import json
list = [ u'adc', u'toto', u'tomato']
print [json.dumps(i).strip('\"') for i in list]

Output:

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

Hope it helps!

Upvotes: 1

Matthew
Matthew

Reputation: 502

Try this:

for item in list:
    for x in range(0, len(item)):
        if item[x] == 'u':
            item[x] = ''

This takes all instances in the list, and checks for the string 'u'. If 'u' is found, than the code replaces it with a blank string, essentially deleting it. Some more code could allow this to check for combinations of letters ('abc', etc.).

Upvotes: 1

Aakash Goel
Aakash Goel

Reputation: 1030

Solution-1

input_list = [ u'adc', u'toto', u'tomato']
output_list=map(lambda x:str(x),input_list )
print output_list

And Output Look like:

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

Solution-2

input_list = [ u'adc', u'toto', u'tomato']
output_list=map(lambda x:x.encode("utf-8"),input_list )
print output_list

And Output Look like:

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

Upvotes: 1

Harshit Garg
Harshit Garg

Reputation: 2259

I verified your question but it says the syntax problem, which means that the way you are declaring the string in the list is not proper. In which case, I have corrected that at line #2.

In [1]: list = [ 'u'adc', 'u'toto', 'u'tomato']
  File "<ipython-input-1-2c6e581e868e>", line 1
    list = [ 'u'adc', 'u'toto', 'u'tomato']
                  ^
SyntaxError: invalid syntax


In [2]: list = [ u'adc', u'toto', u'tomato']

In [3]: list = [ str(item) for item in list ]

In [4]: list
Out[4]: ['adc', 'toto', 'tomato']

In [5]: 

Upvotes: 1

eli-bd
eli-bd

Reputation: 1634

Use "u\'"

For example:

l = [ "u'adc", "u'toto", "u'tomato"]
for item in l:
    print(item.replace("u\'", ""))

Will output:

adc
toto
tomato

Upvotes: 1

Tiny.D
Tiny.D

Reputation: 6556

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']

Upvotes: 2

Related Questions