+ 1

Convert string to list in python3

x = 1,2,3,4,5 How to make list_x = [1,2,3,4,5] or tuple_x = (1,2,3,4,5). I make list_x = [x] but #output is: ['1,2,3,4,5']. So '1,2,3,4,5' is not interger. #edited#

10th Jun 2019, 12:14 PM
Tony Bui
Tony Bui - avatar
5 Answers
+ 10
x = 1, 2, 3, 4, 5 is already a tuple. To turn it into a list use x = list(x). If you're referring to a string x = '1, 2, 3, 4, 5', you can use something like x = list(map(int, x.split(','))) x.split(',') turns the string into a list of strings, map() turns everything into integers, list() puts the items of the map object into a list
10th Jun 2019, 12:43 PM
Anna
Anna - avatar
+ 6
x=1,2,3,4,5 l=[] for i in x: l.append(i) print(l) Thanks
10th Jun 2019, 12:41 PM
Prince PS[Not_Active]
Prince PS[Not_Active] - avatar
0
When you convert a string into lists by using the list() function, you will get a list of each character from the string: print(list("String")) #Output: ["S", "t", "r", "i", "n", "g"]
10th Jun 2019, 4:16 PM
Seb TheS
Seb TheS - avatar
0
x=“1,2,3,4,5” l=[] for i in range(len(x)): if not(x[i]==“,”): l.append(x[i])
2nd Jul 2020, 4:53 AM
Pr0C0d3r
Pr0C0d3r - avatar
- 1
instead of list_x = [x] use list comprehensions list_x = [i for i in x]
10th Jun 2019, 4:47 PM
Choe
Choe - avatar