Convert comma separated string to integer list in Python

Problem

Given a string of comma separated numbers, we want to convert that string to a Python list of integers

Solution

The idea here is to split the string into tokens then convert each token to an integer. We can do that in a couple of ways. Let us see how…

  • Use a list comprehension and the split function using the comma as a delimiter

  • Same as before but using map…

  • Similar approach but using regular expressions

  • Finally, a manual solution

Thanks for visiting. Please use the comment section for questions.

Tags:

Add a Comment

Your email address will not be published. Required fields are marked *