5 Ways to Convert a String List to a NumPy Array

5/5 - (1 vote)

We’ll discuss the following five ways:

  • Method 1: Actually Creating an Array of Strings
  • Method 2: Converting Strings to Float Array
  • Method 3: Converting Strings to Int Array
  • Method 4: How to Convert a Multi-Dimensional List of Strings to a Multi-Dimensional NumPy Array?
  • Method 5: How to Convert a List of Strings to a NumPy Array with a Specific Shape?

Let’s get started! πŸ‘©β€πŸ’»πŸ‘‡

Method 1: Actually Creating an Array of Strings

In the unlikely case that you actually want to convert a list of strings to a NumPy array of strings, you can pass it in the np.array() function.

Here’s a minimal example:

import numpy as np


list_of_strings = ['string1', 'string2', 'string3']
numpy_array = np.array(list_of_strings)

print(numpy_array) 
# ['string1' 'string2' 'string3']

In this code, list_of_strings is your list of strings, and numpy_array is the resulting numpy array.

Method 2: Converting Strings to Float Array

You can convert a list of strings to a numpy array of floats using the numpy.array() function along with the astype() method.

Here’s a concise example:

import numpy as np


list_of_strings = ['1.1', '2.2', '3.3']
numpy_array = np.array(list_of_strings, dtype=float)

print(numpy_array) 
# [1.1 2.2 3.3]

In this code, list_of_strings is your list of strings, and numpy_array is the resulting numpy array of floats. The dtype=float argument in np.array() ensures the conversion to float.

Method 3: Converting Strings to Int Array

You can convert a list of strings to a numpy array of integers using the numpy.array() function along with the dtype parameter.

Here’s a similar example:

import numpy as np


list_of_strings = ['1', '2', '3']
numpy_array = np.array(list_of_strings, dtype=int)

print(numpy_array) 
# [1 2 3]

In this code, list_of_strings is your list of strings, and numpy_array is the resulting numpy array of integers. The dtype=int argument in np.array() ensures the conversion to integer.

Method 4: How to Convert a Multi-Dimensional List of Strings to a Multi-Dimensional NumPy Array?

You can convert a multi-dimensional list of strings to a multi-dimensional numpy array using the numpy.array() function.

Here’s an example:

import numpy as np


lst = [['1', '2'], ['3', '4'], ['5', '6']]
numpy_array = np.array(lst, dtype=int)

print(numpy_array)
'''
[[1 2]
 [3 4]
 [5 6]]
'''

The variable lst is your multi-dimensional list of strings, and numpy_array is the resulting multi-dimensional numpy array of integers. The dtype=int argument in np.array() ensures the conversion to integer. You can change the dtype to float or any other type as per your requirement.

Method 5: How to Convert a List of Strings to a NumPy Array with a Specific Shape?

You can convert a list of strings to a numpy array with a specific shape using the numpy.array() function and then reshape it using the reshape() method.

Example:

import numpy as np


list_of_strings = ['1', '2', '3', '4', '5', '6']
numpy_array = np.array(list_of_strings, dtype=int)

# Reshape to desired shape, for example, (3, 2)
reshaped_array = numpy_array.reshape((3, 2))

print(reshaped_array)
'''
[[1 2]
 [3 4]
 [5 6]]
'''

In this code, list_of_strings is your list of strings, numpy_array is the resulting numpy array of integers, and reshaped_array is the numpy array reshaped to the desired shape. The dtype=int argument in np.array() ensures the conversion to integer. You can change the dtype to float or any other type as per your requirement.

Please note that the total number of elements in the list should be equal to the product of the dimensions specified in the reshape() method. In this case, the list has 6 elements, and the reshape dimensions are 3 and 2, which multiply to 6. If they don’t match, you’ll get an error.

I have created an in-depth guide on the reshape() method that you should check out to improve your NumPy skills:

The Ultimate Guide to NumPy Reshape() in Python