Home > Enterprise >  How to remove string quotes from list in Python
How to remove string quotes from list in Python

Time:01-31

I know there are plenty of titles that sound the same believe me I looked however this has to do with the quotes which make it a string which I have seen very little of and no solutions which worked for me.

I am trying to create a list which i can then put into a database there should normally be a couple hundred items to my list however the problem is the outside quotes turn the entire list into one big element and have had little luck trying to fix it

list with problem

list1 = ['("a"),("b"),("c"),("d"),("e")']

what I need

list1 = [("a"),("b"),("c"),("d"),("e")]


I have tried multiple things so far I have tried...

list1 = ['("a"),("b"),("c"),("d"),("e")']

listFormated = [x.replace("'", '') for x in list1]

instead of removing the outside quote from the list it took all quote marks from inside the string element and removed those instead. for example

list1= ['('a'),('b')('c')("let's")']

now becomes

list1= ['(a),(b),(c),("lets")']

Also Ive tried ast.literal_eval and it almost worked but take a look at the results

from ast import literal_eval

listFormated = [literal_eval(i) for i in list1]

but this returns

[('a','b','c',"let's")]

I would really appreciate any help someone could give.

CodePudding user response:

UPDATED

Is it good enough?

list1 = ['("a"),("b"),("c"),("d"),("e")']
list2 = list1[0].split(',')
list2
>> ['("a")', '("b")', '("c")', '("d")', '("e")']

If you actually need a list of single itemed tuples:

[tuple([x[2]],) for x in list1[0].split(',')]
>> [('a',), ('b',), ('c',), ('d',), ('e',)]

Note: As far as I understand, this is how Python shows tuples with single item in it

CodePudding user response:

This should do what you're trying to do.

list1 = ['("a"),("b"),("c"),("d"),("e")']
list1 = [eval(list1[0])] # since the string is valid python, it can just be evalled

>>> list1
["a", "b", "c", "d", "e"]

Also, why does it contain a string in the first place? It seems like you'd rather have other data types, so why use strings?

CodePudding user response:

From your example, it looks like you have a list with one value, which is a string. Is that really what you want?

Otherwise, if you have the string '("a"),("b"),("c"),...', and want to convert it to a list, you could use the split method:

stringified_list: str = '("a"),("b"),("c")'
list_of_strings: List[str] = stringified_list.split(",")
print(list_of_strings) # Will print ['("a")', '("b")', '("c")']

If you want to return only the strings, you could use ast.literal_eval:

import ast 

stringified_list: str = '("a"),("b"),("c")'
list_of_strings: List[str] = stringified_list.split(",")
list_of_letters: List[str] = [ast.literal_eval(letter) for letter in list_of_strings]

print(list_of_letters) # Will print ['a', 'b', 'c']

Or, if you would like to return tuples of the strings, you could cast the literal_eval(letter) to tuple

list_of_tuples: List[Tuple[str]] = [tuple(ast.literal_eval(letter)) for letter in list_of_strings]
print(list_of_letters) # Will print [('a',), ('b',), ('c',)]

You can read more about ast.literal_eval here

Quick note: If from the begging you had the squared brackets inside your string, you could have used ast.literal_eval on the input:

import ast 

stringified_list: str = '[("a"),("b"),("c")]'
list_of_strings: List[str] = ast.literal_eval(stringified_list)
print(list_of_strings) # Will print ['a', 'b', 'c']

Also, here is why you shouldn't use eval directly.

  • Related