I am reading in a string of integers such as "3 ,2 ,6 "
and want them in the list [3,2,6]
as integers. This is easy to hack about, but what is the "pythonic" way of doing it?
mylist = [int(x) for x in '3 ,2 ,6 '.split(',')]
And if you're not sure you'll only have digits (or want to discard the others):
mylist = [int(x) for x in '3 ,2 ,6 '.split(',') if x.strip().isdigit()]
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