“pythonic” method to parse a string of comma-separated integers into a list of integers?

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()]

Leave a Comment