Use the ast module
>>> import ast
>>> list_as_string = "['item 1', 'item 2', 'item 3']"
>>> _list = ast.literal_eval(list_as_string)
>>> _list
['item 1', 'item 2', 'item 3']
>>>
Answer from C.B. on Stack Overflowpython - string to list and list to string conversion - without split - Stack Overflow
python - Convert string (without any separator) to list - Stack Overflow
Python: how to split string without .split and making it a list
How to convert string to list (NOT list(str), it is how to make list(' "a", 1, 3 )') work)
Videos
To "convert" a string to a list, you can just place it in one, e.g., by creating a new list with []:
input1 = 'I am a boy'
output1 = [input1]
To "convert" a list like you have to a string, you could simply extract the first and only value in it:
input2 = ['I am a boy']
output2 = input2[0]
String to list (supposing it's a list with just 1 string, as per your image)
inp = 'I am a boy'
lst = [input]
List to string (supposing it's a list with just 1 element of type string)
inp = ['I am a boy']
string = input[0]
If you want to force casting (maybe cause you have a list of integers), you can use the str() function, such as str(inp[0]) where inp[0] is maybe a number
Make a list(your_string).
>>> s = "mep"
>>> list(s)
['m', 'e', 'p']
You mean that you want something like:
''.join(n for n in phone_str if n.isdigit())
This uses the fact that strings are iterable. They yield 1 character at a time when you iterate over them.
Regarding your efforts,
This one actually removes all of the digits from the string leaving you with only non-digits.
x = row.translate(None, string.digits)
This one splits the string on runs of whitespace, not after each character:
list = x.split()
Im trying to split a string without .split or .partition and get the first word before a space. Any ideas? Thanks
Given I have the following string
a = '["b", "s", 1]'
How would I convert it to an actual list? (If I do print(type(a)) it will say <class 'str'>, but I want to print <class 'list'> )
One way would be to use a for loop with string.split(',') along with a small bit of processing to remove the brackets and stuff. but I have some reasons (more like assumptions) for not doing so:
-
I might be working on a LONG list per user which is already pretty slow, and maybe, just maybe, if I get alot of requests from my API it might really slow down the processing. (I have to remove duplicates too after this, I can't do
OrderedSet(list)because it won't keep the insertion order.) -
I am going to be hosting this on Heroku (its a flask API and well this might FURTHER slow down the processing).
The above reasons are really my assumptions, not really researched reasons (so they might be incorrect)
So what is an efficient, fast way to do this on a flask server (even if the list is long)? Could somehow numpy arrays help me? ( it will be between 250-500 items)
sentence = 'This is a sentence'
split_value = []
tmp = ''
for c in sentence:
if c == ' ':
split_value.append(tmp)
tmp = ''
else:
tmp += c
if tmp:
split_value.append(tmp)
You can use regular expressions if you want:
>>> import re
>>> s = 'This is a Sentence'
>>> re.findall(r'\S+', s)
['This', 'is', 'a', 'Sentence']
The \S represents any character that isn't whitespace, and the + says to find one or more of those characters in a row. re.findall will create a list of all strings that match that pattern.
But, really, s.split() is the best way to do it.