Use int which converts a string to an int, inside a list comprehension, like this:

desired_array = [int(numeric_string) for numeric_string in current_array]
Answer from sepp2k on Stack Overflow
🌐
Java2Blog
java2blog.com › home › python › convert string array to int array in python
Convert String Array to Int Array in Python [3 ways] - Java2Blog
December 25, 2022 - The eval() function accepts a Python string and evaluates it as a Python expression. We can use it in place of the int() function in the previous method. ... Use the map() function to apply the int() function to all elements of the array. Use the list() function to convert the map type object ...
Discussions

How to convert string list to int list?
This is not how you would do this in Python. Any time you see code with range(len(something)), you know you are doing it wrong. The way to do it would be this: numbers = [int(num) for num in numbers] More on reddit.com
🌐 r/learnpython
8
5
June 4, 2023
Convert string array into integer python - Stack Overflow
I need to convert following string array into integers. How to do it in python. More on stackoverflow.com
🌐 stackoverflow.com
python - Converting a string to an array of integers - Code Review Stack Exchange
To make this work, we first throw ... numbers, we then make sure to explicitly convert those strings to numbers, using the int() casting function and list comprehension. ... \$\begingroup\$ You should refrain from using Python builtins names for your variables str, list and ... More on codereview.stackexchange.com
🌐 codereview.stackexchange.com
June 11, 2021
Converting a single line string to integer array in Python - Stack Overflow
I'm trying to learn python through HackerRank, and I've been stuck on reading stdin. The problem gives an array of integers as a single line of text formatted like: 1 2 3 4 5 This should become the More on stackoverflow.com
🌐 stackoverflow.com
🌐
GeeksforGeeks
geeksforgeeks.org › python › python-converting-all-strings-in-list-to-integers
Converting all Strings in a List to Integers - Python - GeeksforGeeks
July 11, 2025 - map() function applies a given function to all elements in an iterable. Here, we use map(int, a) to convert each string in the list to an integer. ... List comprehension provides a more Pythonic and concise way to convert a list of strings to ...
Top answer
1 of 2
4

Use int with base=16 for conversion, and list comprehension for output list:

[int(num, base=16) for num in nums]

Example:

In [376]: nums = ['02', '01', '03', '01', '04', 'ab', 'cd']                                                                                                                                                 

In [377]: [int(num, base=16) for num in nums]                                                                                                                                                               
Out[377]: [2, 1, 3, 1, 4, 171, 205]
2 of 2
1

You can use a list comprehension for this

>>> data = ['02', '01', '03', '01', '04', '01', '05', '01', '06', '01', '07', '01', '08', '01', '09', '01', '0a', '01', '0b', '01', '0c', '01', '0d', '01', '0e', '01', '0f', '01', '10', '01', '11', '01', '12', '01', '13', '01', '14', '01', '03', '02', '04', '02', '05', '02', '06', '02', '07', '02', '08', '02', '09', '02', '0a', '02', '0b', '02', '0c', '02', '0d', '02', '0e', '02', '0f', '02', '10', '02', '11', '02', '12', '02', '13', '02', '14', '02', '04', '03', '05', '03', '06', '03', '07', '03', '08', '03', '09', '03', '0a', '03', '0b', '03', '0c', '03', '0d', '03', '0e', '03', '0f', '03', '10', '03', '11', '03', '12', '03', '13', '03', '14', '03', '05', '04', '06', '04', '07', '04', '08', '04', '09', '04', '0a', '04', '0b', '04', '0c', '04', '0d', '04', '0e', '04', '0f', '04', '10', '04', '11', '04', '12', '04', '13', '04', '14', '04', '06', '05', '07', '05', '08', '05', '09', '05', '0a', '05', '0b', '05', '0c', '05', '0d', '05', '0e', '05', '0f', '05', '10', '05', '11', '05', '12', '05', '13', '05', '14', '05', '07', '06', '08', '06', '09', '06', '0a', '06', '0b', '06', '0c', '06', '0d', '06', '0e', '06', '0f', '06', '10', '06', '11', '06', '12', '06', '13', '06', '14', '06', '08', '07', '09', '07', '0a', '07', '0b', '07', '0c', '07', '0d', '07', '0e', '07', '0f', '07', '10', '07', '11', '07', '12', '07', '13', '07', '14', '07', '09', '08', '0a', '08', '0b', '08', '0c', '08', '0d', '08', '0e', '08', '0f', '08', '10', '08', '11', '08', '12', '08', '13', '08', '14', '08', '0a', '09', '0b', '09', '0c', '09', '0d', '09', '0e', '09', '0f', '09', '10', '09', '11', '09', '12', '09', '13', '09', '14', '09', '0b', '0a', '0c', '0a', '0d', '0a', '0e', '0a', '0f', '0a', '10', '0a', '11', '0a', '12', '0a', '13', '0a', '14', '0a', '0c', '0b', '0d', '0b', '0e', '0b', '0f', '0b', '10', '0b', '11', '0b', '12', '0b', '13', '0b', '14', '0b', '0d', '0c', '0e', '0c', '0f', '0c', '10', '0c', '11', '0c', '12', '0c', '13', '0c', '14', '0c', '0e', '0d', '0f', '0d', '10', '0d', '11', '0d', '12', '0d', '13', '0d', '14', '0d', '0f', '0e', '10', '0e', '11', '0e', '12', '0e', '13', '0e', '14', '0e', '10', '0f', '11', '0f', '12', '0f', '13', '0f', '14', '0f', '11', '10', '12', '10', '13', '10', '14', '10', '12', '11', '13', '11', '14', '11', '13', '12', '14', '12', '14', '13']
>>> [int(i, base=16) for i in data]
[2, 1, 3, 1, 4, 1, 5, 1, 6, 1, 7, 1, 8, 1, 9, 1, 10, 1, 11, 1, 12, 1, 13, 1, 14, 1, 15, 1, 16, 1, 17, 1, 18, 1, 19, 1, 20, 1, 3, 2, 4, 2, 5, 2, 6, 2, 7, 2, 8, 2, 9, 2, 10, 2, 11, 2, 12, 2, 13, 2, 14, 2, 15, 2, 16, 2, 17, 2, 18, 2, 19, 2, 20, 2, 4, 3, 5, 3, 6, 3, 7, 3, 8, 3, 9, 3, 10, 3, 11, 3, 12, 3, 13, 3, 14, 3, 15, 3, 16, 3, 17, 3, 18, 3, 19, 3, 20, 3, 5, 4, 6, 4, 7, 4, 8, 4, 9, 4, 10, 4, 11, 4, 12, 4, 13, 4, 14, 4, 15, 4, 16, 4, 17, 4, 18, 4, 19, 4, 20, 4, 6, 5, 7, 5, 8, 5, 9, 5, 10, 5, 11, 5, 12, 5, 13, 5, 14, 5, 15, 5, 16, 5, 17, 5, 18, 5, 19, 5, 20, 5, 7, 6, 8, 6, 9, 6, 10, 6, 11, 6, 12, 6, 13, 6, 14, 6, 15, 6, 16, 6, 17, 6, 18, 6, 19, 6, 20, 6, 8, 7, 9, 7, 10, 7, 11, 7, 12, 7, 13, 7, 14, 7, 15, 7, 16, 7, 17, 7, 18, 7, 19, 7, 20, 7, 9, 8, 10, 8, 11, 8, 12, 8, 13, 8, 14, 8, 15, 8, 16, 8, 17, 8, 18, 8, 19, 8, 20, 8, 10, 9, 11, 9, 12, 9, 13, 9, 14, 9, 15, 9, 16, 9, 17, 9, 18, 9, 19, 9, 20, 9, 11, 10, 12, 10, 13, 10, 14, 10, 15, 10, 16, 10, 17, 10, 18, 10, 19, 10, 20, 10, 12, 11, 13, 11, 14, 11, 15, 11, 16, 11, 17, 11, 18, 11, 19, 11, 20, 11, 13, 12, 14, 12, 15, 12, 16, 12, 17, 12, 18, 12, 19, 12, 20, 12, 14, 13, 15, 13, 16, 13, 17, 13, 18, 13, 19, 13, 20, 13, 15, 14, 16, 14, 17, 14, 18, 14, 19, 14, 20, 14, 16, 15, 17, 15, 18, 15, 19, 15, 20, 15, 17, 16, 18, 16, 19, 16, 20, 16, 18, 17, 19, 17, 20, 17, 19, 18, 20, 18, 20, 19]

When you convert to int there is a second parameter that specifies the base, 16 in this case for hex.

Top answer
1 of 4
4

Nice solution, a few suggestions:

  • The function name convert_to_int_array seems too general. Consider a more specific name, for example, extract_numbers, or something similar.
  • There is no need to initialize results_as_array to None.
  • Mapping a list of strings to integers:
    for index in range(0, len(results_as_array)):
        results_as_array[index] = int(results_as_array[index].strip())
    return results_as_array
    
    looks like the job for map:
    return list(map(int, results_as_array))
    
    Since the result of map is a generator, it needs to be converted to a list with list.
  • Tests: I noticed this line:
    draw_result = "04-15-17-25-41" # change to "01-12 + 08-20" or "04-15-17-25-41" or "03-23-27-34-37, Mega Ball: 13" to test
    
    testing by changing draw_result manually it is time-consuming. A better way would be to keep the tests in a dictionary and to use assert. For example:
    tests = {
          "04-15-17-25-41": [4, 15, 17, 25, 41],
          "01-12 + 08-20": [1, 12, 8, 20],
          "03-23-27-34-37, Mega Ball: 13": [3, 23, 27, 34, 37]
    }
    
    for lottery_string, expected_output in tests.items():
          assert expected_output == convert_to_int_array(lottery_string)
    
    If you want to explore more "unit testing" have a look at unittest.

Alternative approach:

  1. Remove , Mega Ball: 13 if exists
  2. Extract all numbers with a regular expression
import re

def extract_numbers(draw_result):
    draw_result = draw_result.split(',')[0]
    matches = re.findall(r'\d+', draw_result)
    return list(map(int, matches))
2 of 4
5

Marc's answer is an excellent option if you don't care about validation.

If you need to be somewhat sure that the given string represents one of the known formats, then you can pre-bind a match method in a list of pre-compiled regular expressions:

import re
from typing import Iterable

lottery_searches = [
    re.compile(pat).match
    for pat in (
        r'^(\d+)-(\d+)-(\d+)-(\d+)-(\d+), Mega Ball.*',
        r'^(\d+)-(\d+)-(\d+)-(\d+)-(\d+)$',
    )
]


def lottery_string_to_ints(lottery: str) -> Iterable[int]:
    for search in lottery_searches:
        match = search(lottery)
        if match:
            return (int(g) for g in match.groups())

    raise ValueError(f'"{lottery}" is not a valid lottery string')

with output

In[2]: tuple(lottery_string_to_ints('03-23-27-34-37, Mega Ball: 13'))
Out[2]: (3, 23, 27, 34, 37)

In[3]: tuple(lottery_string_to_ints('01-12 + 08-20'))
Out[3]: (1, 12, 8, 20)

In[4]: tuple(lottery_string_to_ints('04-15-17-25-41'))
Out[4]: (4, 15, 17, 25, 41)

In[5]: tuple(lottery_string_to_ints('04-15'))
Traceback (most recent call last):
  File "262933.py", line 20, in lottery_string_to_ints
    raise ValueError(f'"{lottery}" is not a valid lottery string')
ValueError: "04-15" is not a valid lottery string
🌐
Analytics Vidhya
analyticsvidhya.com › home › 6 methods to convert string to int in python
Top 6 Ways to Convert String to Int in Python
May 20, 2025 - Another method to convert a string to an integer is using the ast.literal_eval() function from the ast module. This function evaluates the given string as a Python literal and returns the corresponding value.
Find elsewhere
🌐
Spark By {Examples}
sparkbyexamples.com › home › python › python convert list of strings to ints
Python Convert List of Strings to Ints - Spark By {Examples}
2 weeks ago - How to convert a list of strings to ints in Python? You can use list comprehension or the map() function to convert a list of strings to ints in one line
🌐
DaniWeb
daniweb.com › programming › software-development › threads › 274089 › python-converting-string-to-integer
PYTHON: Converting String to Integer!!! [SOLVED] | DaniWeb
April 8, 2010 - firstNumber = int(array[0]) secondNumber = int(array[1]) thirdNumber = int(array[2]) ...#etc. etc. As you can see, this conversion process (from String to Integer) is very inefficient as you must convert each cell one by one... Is there a more efficient alternative solution to this conversion process??? Cheers, ThompsonSensibl · python ·
🌐
YouTube
youtube.com › watch
Converting a String Array to an int Array in Python: A Step-By-Step Guide - YouTube
Discover how to convert a string array of coordinates into an `int` array in Python for your data processing needs, complete with easy-to-follow steps and co...
Published: May 26, 2025
Views: 0
🌐
Vultr Docs
docs.vultr.com › python › third party › numpy › fromstring()
Python Numpy fromstring() - Convert String to Array | Vultr Docs
November 18, 2024 - The fromstring() function in Python's NumPy library is a powerful tool for converting string data into numerical arrays quickly and efficiently.
🌐
YouTube
youtube.com › watch
Python Tips: Converting a string list to an integer list, in 1 line - YouTube
A simple easy way to turn a list of strings into integers in python, all in one line.
Published: March 8, 2021
🌐
Stack Overflow
stackoverflow.com › questions › 68210530 › converting-string-array-into-int-array-in-python
Converting string array into int array in python - Stack Overflow
July 1, 2021 - Initiating read file from another script, I need to get this input as integer, so that I can use it as input to make np.array.
🌐
NumPy
numpy.org › doc › 2.0 › reference › generated › numpy.fromstring.html
numpy.fromstring — NumPy v2.0 Manual
If string contains unicode text, the binary mode of fromstring will first encode it into bytes using utf-8, which will not produce sane results. ... Reference object to allow the creation of arrays which are not NumPy arrays. If an array-like passed in as like supports the __array_function__ protocol, the result will be defined by it.
🌐
GeeksforGeeks
geeksforgeeks.org › python › convert-string-to-integer-in-python
Convert String to Int in Python - GeeksforGeeks
The simplest way to convert a string to an integer in Python is by using the int() function.
Published: June 15, 2026