I have a list
list = ['1,2,3,4,5,6,7']
I want my list as under:
desire output = [1,2,3,4,5,6,7]
how to get this, i read other similar answer where strip method used. i had tried strip but strip method not work with list, it work with strings.
'list' object has no attribute 'strip'
CodePudding user response:
What you have is a list
with a single str
element. To get the list you want, try:
>>> list(map(int, lst[0].split(",")))
CodePudding user response:
You can use a nested list-comprehension to split each item in the list, convert the sub-items to ints, and then flatten any sub-lists:
>>> my_list = ['1,2,3,4,5,6,7', '8,9,10']
>>> [i for sub_list in my_list for i in [int(i) for i in sub_list.split(',')]]
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]