This question already has answers here:
How to convert string representation of list to a list
(19 answers)
Closed 1 year ago.
Let's assume I have list, and have turned it into a string (to use it as a dictionary key for example). Is there a way to get the list back from the string? This code snippet should illustrate what I want:
list_str = str([1,2,3])
my_list = some_operation(list_str)
such that the variable my_list contains the list [1,2,3]
Any help would be appreciated!
You could use ast.literal_eval(list_str) but the real question is why did you convert it into a string in the first place? You could have converted it to a tuple (immutable and hashable) to use as a dict key
It's not a good idea, but you can try:
# list_str holds your list in a string
vals = list_str[1:-1]
l = [int(x.strip()) for x in vals.split(",")]
Related
This question already has answers here:
"pythonic" method to parse a string of comma-separated integers into a list of integers?
(3 answers)
Closed 12 months ago.
I have this list of strings:
data = ["23444,2345,5332,2534,3229"]
Is there a way I can convert it to list of int, like this:
[23444,2345,5332,2534,3229]
Access the first element Then split string at , and map with integer datatype it returns a lazy iterator convert it into list
data = list(map(int,data[0].split(",")))
A simple list comprehension should do it:
data = ["23444,2345,5332,2534,3229"]
newdata = [int(v) for v in data[0].split(',')]
print(newdata)
...or with map...
newdata = list(map(int, data[0].split(',')))
Output:
[23444, 2345, 5332, 2534, 3229]
Create a variable to hold your new values and simply loop through the values and convert each to int.
For example
strings = ["2","3","4","5","6"]
ints = []
for i in strings:
ints.append(int(i))
This question already has answers here:
How to use Python sets and add strings to it in as a dictionary value
(2 answers)
Closed 4 years ago.
I am trying to convert a string to a set containing that string. How can I do this without splitting?
when I write:
set("abc")
the result is:
{'a','b','c'}
but I want it to be:
{"abc"}
Doku set(iterable) will create a set of each element of an iterable - strings are iterable - hence you create a set of the characters of the string.
If you want whole strings, use
k = {"mystring",}
or
k = set( ["mystring"] ) # wrap your string into another iterable
This question already has answers here:
Converting a string of tuples to a list of tuples in Python
(3 answers)
Closed 5 years ago.
I have a list of tuples saved into a string (unfortunately). I am looking for a fast an efficient way to convert this string to an actual list of tuples.
Example:
mylist_string = '[(40.7822603, -73.9525339), (40.7142, -74.0087), (40.7250027, -73.9413106), (40.703422, -73.9862948), (40.7169963, -74.0149991), (40.7420448, -73.9918131), (40.7287, -73.9799), (40.7757237, -73.9492357), (40.7169904, -73.9578252), (40.726103, -73.9780367), (40.7776792, -73.9585829), (40.6750972, -73.9679734), (40.6867687, -73.9743078), (40.6684762, -73.9755826), (40.7169, -73.9578), (40.6996798, -73.9291393), (40.6680182, -73.9809183), (40.7346, -74.0073), (40.6871087, -73.9741862), (40.7160416, -73.9452393), (40.7178984, -74.0063829)]'
the expected output is a list of tuples in Python.
Thanks
This is one way:
import ast
ast.literal_eval(mystr)
# [(40.7822603, -73.9525339),
# (40.7142, -74.0087),
# (40.7250027, -73.9413106),
# (40.703422, -73.9862948),
# ...
# (40.6871087, -73.9741862),
# (40.7160416, -73.9452393),
# (40.7178984, -74.0063829)]
eval(mylist_string)
This will evaluate your string as if you typed it in Python.
Edit: this is an answer to the question, but it is not a good answer. There are security risks and it might not do what you think it does. So don't use eval, use:
import ast
ast.literal_eval(mystr)
See [this SO question] for a detailed comparison.
This question already has answers here:
Convert all strings in a list to integers
(11 answers)
Closed 6 years ago.
Apologies if this question has already been covered!I am trying to convert each element in this list of listsinto integer format. I have used two for loops to iterate through the list, in the same way we do it "C" There is an error that says " Object is not subscriptable" in Line 3 . Am i missing something obvious?
Here is the code:
l=[['1','2'],['3','4']]
for i in range(len(l)):
for j in range(len[i]):
l[j]=int(l[j])
You can use nested list comprehensions:
outer = [['1','2'],['3','4']]
outer_as_ints = [[int(x) for x in inner] for inner in outer]
This question already has answers here:
Converting a string representation of a list into an actual list object [duplicate]
(3 answers)
Closed 8 years ago.
I am pretty confused here.
I have a function that has a list as an argument. It then does list-specific functions on it. Here is an example:
def getValues( my_list ):
for q in my_list:
print(q)
But, I get my list from USER INPUT like this:
a_list = input("Please enter a list. ")
getValues(a_list)
The built-in input() function returns a string, not a list. My solution is to take that string of the list and turn it back into a list.
Only I don't know how.
Thanks!
Its what that ast.literal_eval is for :
>>> ast.literal_eval('[1,2,3]')
[1, 2, 3]