I want to convert
['60,78', '70,77', '80,74', '90,75', '100,74', '110,75']
in to
['60', '78', '70', '77'.. etc]
I thought I could use
for word in lines:
word = word.split(",")
newlist.append(word)
return newlist
but this produces this instead:
[['60', '78'], ['70', '77'], ['80', '74'], ['90', '75'], ['100', '74'], ['110', '75']]
Can anyone please offer a solution?
You need to use list.extend
instead of list.append
.
newlist = []
for word in lines:
word = word.split(",")
newlist.extend(word) # <----
return newlist
Or, using list comprehension:
>>> lst = ['60,78', '70,77', '80,74', '90,75', '100,74', '110,75']
>>> [x for xs in lst for x in xs.split(',')]
['60', '78', '70', '77', '80', '74', '90', '75', '100', '74', '110', '75']
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With