abcd
abcd

Reputation: 19

Splitting list in list and converting list into an integer list

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?

Upvotes: 0

Views: 72

Answers (2)

TigerhawkT3
TigerhawkT3

Reputation: 49320

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]

Upvotes: 0

hashcode55
hashcode55

Reputation: 5860

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]

Upvotes: 4

Related Questions