JimmyOppa
JimmyOppa

Reputation: 67

Modify a part of the item in the list

So, I have a list of items like,

mylist = ['apple', '"banana"', 'orange']

I want to remove double quotes from the item banana in mylist. The resulting list should look like this,

myresultlist = ['apple' , 'banana', 'orange']

I have tried function i.replace('"', "") but no luck. Is it possible?

Upvotes: 1

Views: 56

Answers (2)

Dan Curry
Dan Curry

Reputation: 149

mylist = ['apple', '"banana"', 'orange']

newlist = []

for item in mylist:
    newlist.append(item.strip('"'))

print(newlist)

Upvotes: 1

Andrej Kesely
Andrej Kesely

Reputation: 195418

Try str.replace and list-comprehension:

mylist = ["apple", '"banana"', "orange"]

mylist = [v.replace('"', "") for v in mylist]
print(mylist)

Prints:

['apple', 'banana', 'orange']

Upvotes: 5

Related Questions