You may want to use the ndarray.item method, as in a.item(). This is also equivalent to (the now deprecated) np.asscalar(a). This has the benefit of working in situations with views and superfluous axes, while the above solutions will currently break. For example,

>>> a = np.asarray(1).view()
>>> a.item()  # correct
1

>>> a[0]  # breaks
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
IndexError: too many indices for array


>>> a = np.asarray([[2]])
>>> a.item()  # correct
2

>>> a[0]  # bad result
array([2])

This also has the benefit of throwing an exception if the array is not actually a scalar, while the a[0] approach will silently proceed (which may lead to bugs sneaking through undetected).

>>> a = np.asarray([1, 2])
>>> a[0]  # silently proceeds
1
>>> a.item()  # detects incorrect size
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: can only convert an array of size 1 to a Python scalar
Answer from Aaron Voelker on Stack Overflow
🌐
IncludeHelp
includehelp.com › python › convert-list-or-numpy-array-of-single-element-to-float.aspx
Python - Convert list or NumPy array of single element to float
December 23, 2023 - # Converting to float (using index 0) res = float(arr[0]) # Display result print("Result:\n", res) '''
Discussions

python - How do I convert all of the items in a list to floats? - Stack Overflow
I have a script which reads a text file, pulls decimal numbers out of it as strings and places them into a list. So I have this list: my_list = ['0.49', '0.54', '0.54', '0.55', '0.55', '0.54', '0.5... More on stackoverflow.com
🌐 stackoverflow.com
No way i am trying lets me convert string list into float list
Format your code More on reddit.com
🌐 r/learnpython
8
1
October 25, 2023
python - How to convert one element lists to float - Stack Overflow
Connect and share knowledge within a single location that is structured and easy to search. Learn more about Teams · Get early access and see previews of new features. Learn more about Labs ... I'm trying to extract pressure values from openFoam output log. I read the lines of file as strings and extract floats ... More on stackoverflow.com
🌐 stackoverflow.com
February 4, 2020
string - in Python how to convert number to float in a mixed list - Stack Overflow
1 How to convert only numbers in a mixed list into float · 2 Casting a list of strings into float if possible. - python More on stackoverflow.com
🌐 stackoverflow.com
🌐
Delft Stack
delftstack.com › home › howto › python › convert list to float python
How to Convert List to Float in Python | Delft Stack
February 2, 2024 - This ensures that each element in the list is explicitly cast to a floating-point number. Finally, we print float_lst, which results in the output: [1.5, 2.0, 2.5], representing the list of the converted floating-point values. The output reflects the original list but with data types changed to float, facilitating numerical operations. Using the map() function in Python allows us to apply a specific function to every item in an iterable (such as a list) and get a new iterable containing the transformed items.
🌐
GeeksforGeeks
geeksforgeeks.org › python › python-convert-float-string-list-to-float-values
Convert Float String List to Float Values-Python - GeeksforGeeks
July 15, 2025 - The task of converting a list of ... in Python involves changing the elements of the list, which are originally represented as strings, into their corresponding float data type. For example, given a list a = ['87.6', '454.6', '9.34', '23', '12.3'], the goal is to convert each string into a float, resulting in the list [87.6, 454.6, 9.34, 23.0, 12.3]. List comprehension provides a concise way to convert each string in the list to a float. It uses a single line of code ...
🌐
YouTube
youtube.com › codefix
convert list to float python - YouTube
Download this code from https://codegive.com Title: Converting a List to Float in Python: A Step-by-Step TutorialIntroduction:In Python, you may encounter sc...
Published   December 11, 2023
Views   34
🌐
w3resource
w3resource.com › python-exercises › basic › python-basic-1-exercise-140.php
Python: Convert all items in a given list to float values - w3resource
May 24, 2025 - A for loop iterates through each item in the original list (nums). Each string item is converted to a float using the "float()" function, and the result is appended to 'nums_of_floats'.
🌐
Finxter
blog.finxter.com › how-to-convert-a-string-list-to-a-float-list-in-python
How to Convert a String List to a Float List in Python – Be on the Right Side of Change
April 8, 2021 - The most Pythonic way to convert a list of strings to a list of floats is to use the list comprehension floats = [float(x) for x in strings]. It iterates over all elements in the list and converts each list element x to a float value using the float(x) built-in function.
🌐
YouTube
youtube.com › pythongpt
Convert list or numpy array of single element to float in python - YouTube
Download this code from https://codegive.com Title: Converting a List or NumPy Array of Single Elements to Float in PythonIntroduction:In Python, it's common...
Published   November 23, 2023
Views   5
Find elsewhere
🌐
DaniWeb
daniweb.com › programming › software-development › threads › 175435 › convert-list-of-int-to-floats
python - convert list of int to floats [SOLVED] | DaniWeb
February 17, 2009 - You should be already familiar with the for loop which loops through each integer in the list and assigns it to the variable i. The first expression simply tells the comprehension what value to append to the new list; the value here is float(i). So essentially, the comprehension loops through each integer in the list, converts it to a float, and then appends it to a new one which is the assigned to l2. ... Just an observation, I would avoid using variable names like l1, l2 and so on, they look a lot like numbers 11, 12. Here is an example how to use Python function map(): q = [1, 2, 3, 4, 5] # map applies float() to every element in the list q qf = map(float, q) print(qf) # [1.0, 2.0, 3.0, 4.0, 5.0]
🌐
Finxter
blog.finxter.com › home › learn python blog › how to convert an integer list to a float list in python
How to Convert an Integer List to a Float List in Python - Be on the Right Side of Change
August 22, 2022 - The most Pythonic way to convert a list of integers ints to a list of floats is to use the list comprehension expression floats = [float(x) for x in ints]. It iterates over all elements in the list ints using list comprehension and converts each list element x to a float value using the float(x) ...
🌐
YouTube
youtube.com › hey delphi
Array : Convert list or numpy array of single element to float in python - YouTube
Array : Convert list or numpy array of single element to float in pythonTo Access My Live Chat Page, On Google, Search for "hows tech developer connect"I pro...
Published   May 1, 2023
Views   2
🌐
TutorialsPoint
tutorialspoint.com › python-program-to-convert-elements-in-a-list-of-tuples-to-float
Python program to convert elements in a list of Tuples to Float
my_list = [("31", "py"), ("22", "226.65"), ("18.12", "17"), ("pyt", "12")] print("The list is :") print(my_list) my_result = [] for index in my_list: my_temp = [] for element in index: if element.isalpha(): my_temp.append(element) else: my_temp.append(float(element)) my_result.append((my_temp[0],my_temp[1])) print("The result is :") print(my_result) The list is : [('31', 'py'), ('22', '226.65'), ('18.12', '17'), ('pyt', '12')] The result is : [(31.0, 'py'), (22.0, 226.65), (18.12, 17.0), ('pyt', 12.0)] A list of list with integers is defined and is displayed on the console. An empty list is declared. The list is iterated over, and the element is checked for alphabet using isalpha() function.
🌐
Reddit
reddit.com › r/learnpython › no way i am trying lets me convert string list into float list
r/learnpython on Reddit: No way i am trying lets me convert string list into float list
October 25, 2023 -

Not able to convert the "close_list" list into a float? list? I've tried a lot of ways that I see on the internet a lot that it's too much to put in here, and i am still getting an error of:" Exception has occurred: TypeError:list indices must be integers or slices, not float File "{filename}\import csv.py", line 25, in <module> diff = close_list[i] - price_compare TypeError: list indices must be integers or slices, not float ".

Am i missing anything that needs to be declared or imported, updated extensions? I am doing exactly what the internet says and even putting in "import numpy as np" and that's not working and am getting real disappointed. i have tried updating it and numpy is already up to date. How can i modify this so i can actually subtract price_compare from close_list[i].

import csv
import os
import sys
import numpy as np

close_list = ['0.0', '1.0', '2.5', '2.0']
len = len(close_list)
price_compare = [close_list[0]]
diff = 0.0
price=0
# [float(i) for i in value]
diff_close_list = []
for i in range(1,len):
i = float(i)
# price_compare = float(price_compare)
diff = close_list[i] - price_compare
price_compare = i
print(type(i))
print(i)
print(type(price_compare))
print(price_compare)
print(type(diff))
print(diff)

diff_close_list.append(diff)
# print(diff_close_list)

🌐
Python-Fiddle
python-fiddle.com › challenges › convert-list-elements-to-floats
Convert List Elements to Floats - Python Challenge
def convert_to_floats(data): """ Convert all convertible elements in a list of lists to floats. Args: data (list of lists): A list containing sublists with string elements. Returns: list of lists: A new list of lists with convertible elements converted to floats.
🌐
Scaler
scaler.com › home › topics › how to convert string to float in python?
Convert String to Float in Python - Scaler Topics
May 5, 2022 - For converting the list of strings ... have to iterate the string list and take the values one by one and then convert the string values to the floating values and then append all the float values to the floating value list...
🌐
GeeksforGeeks
geeksforgeeks.org › python › python-ways-to-convert-array-of-strings-to-array-of-floats
Python | Ways to convert array of strings to array of floats - GeeksforGeeks
July 11, 2025 - Here is the approach to convert ... array to store the converted floats. Loop through each element of the input array and convert the current element to a float using the float() function....
Top answer
1 of 2
2

You can use sum() with a generator expression to convert each number in the list to float utilizing str.join:

nums = [
    ['2', '.', '7'],
    ['۳'],
    ['۳', '۰', '۶'],
    ['۷', '۴'],
    ['۵'],
    ['۹', '۰'],
    ['۱', '۰'],
    ['۱', '۵'],
    ['۲', '۶'],
]

for num in nums:
  print(f'{num} =', float(''.join(num)))

total = sum(float(''.join(num)) for num in nums)
print(f'{total = }')

Output:

['2', '.', '7'] = 2.7
['۳'] = 3.0
['۳', '۰', '۶'] = 306.0
['۷', '۴'] = 74.0
['۵'] = 5.0
['۹', '۰'] = 90.0
['۱', '۰'] = 10.0
['۱', '۵'] = 15.0
['۲', '۶'] = 26.0
total = 531.7
2 of 2
1

Based on what you've provided, you're looking to:

  • Accumulate numbers (Persian and Arabic) from a .docx into a list
  • Sum that list and return a float

In addition to the solutions already suggested, this is another way to do it:

from docx import Document
import re

document = Document("File.docx")
text = " ".join(paragraph.text for paragraph in document.paragraphs)

# Find numbers, converting the Persian numbers to Arabic
persian_to_arabic = str.maketrans("۰۱۲۳۴۵۶۷۸۹", "0123456789")
numbers = re.findall(pattern=r"-?\d+\.\d+|-?\d+|[۰۱۲۳۴۵۶۷۹]+", string=text)
converted_numbers = [num.translate(persian_to_arabic) for num in numbers]

# Sum the list and return a float
total = sum(map(float, converted_numbers))
print(total)

Testing with the following input:

document = Document()
document.add_paragraph("The total cost is $45.67 and the discount is $5.50.")
document.add_paragraph("The Persian digits for 123 are ۱۲۳.")
document.add_paragraph("A negative number like -123.45 should be considered.")

This gives us ['45.67', '5.50', '123', '123', '-123.45'], which sums to 173.72000000000003.