I am trying to spilt the following list so that I can use it for further purposes: How to split ['1,1','2,2'] into [1,1,2,2] in python?
Asked
Active
Viewed 66 times
0
-
2Possible duplicate of [Convert all strings in a list to int](http://stackoverflow.com/questions/7368789/convert-all-strings-in-a-list-to-int) – depperm Feb 27 '17 at 13:29
-
split? This is simply `[int(x) for x in thelist]`... – Willem Van Onsem Feb 27 '17 at 13:30
-
3It would be useful if you show what code you have tried so we can help build up your knowledge. – pstatix Feb 27 '17 at 13:30
-
`list(itertools.chain.from_iterable([[int(i) for i in item.split(',')] for item in lst]))` – Chris_Rands Feb 27 '17 at 13:36
-
Don't use this one: `eval(str(lst).replace("'",""))` – Chris_Rands Feb 27 '17 at 13:42
-
@abcd if any of the answers helped you, mark it as correct. – hashcode55 Feb 27 '17 at 13:59
2 Answers
4
This list comprehension would work -
s = ['1, 1', '2, 2']
print [int(j) for i in s for j in i.split(',')]
Output
[1, 1, 2, 2]

hashcode55
- 5,622
- 4
- 27
- 40
0
Join the list on ','
and then split it back up and map to integers:
>>> l = ['1,1', '2,2']
>>> list(map(int, ','.join(l).split(',')))
[1, 1, 2, 2]

TigerhawkT3
- 48,464
- 6
- 60
- 97