So, your error is that instead of simply adding your latitute to the list, you are iterating over each character of the latitude, as a string, and adding that character to a list.

result=[]
for value in response_i['objcontent'][0]['rowvalues']:
    lat = value[0]
    print(lat)
    result.append(float(lat))

print (result)

Besides that, using range(len(...))) is the way things have to be done in almost all modern languages, because they either don't implement a "for ...each" or do it in an incomplete or faulty way.

In Python, since the beginning it is a given that whenever one wants a for iteration he wants to get the items of a sequence, not its indices (for posterior retrieval of the indices). Some auxiliar built-ins come in to play to ensure you just interate the sequence: zip to mix one or more sequences, and enumerate to yield the indices as well if you need them.

Answer from jsbueno on Stack Overflow
๐ŸŒ
Esri Community
community.esri.com โ€บ t5 โ€บ python-questions โ€บ how-do-i-create-a-list-of-floats-in-python โ€บ td-p โ€บ 516938
How do I create a list of floats in Python? - Esri Community
December 11, 2021 - Going further back in your code, your code to initialize a list of floats is not doing what you think: ... The code as written is creating a list with a single item, the item being a string with a single character, 'f'. You can initialize a float array, but the syntax is different: >>> import array >>> approxTen = array.array('f') >>> approxTen array('f') >>> โ€โ€โ€โ€โ€ ยท Lists are more common in Python than arrays because of their flexibility with data types and syntactic sugar for working with them, like list comprehensions.
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ python โ€บ python-create-a-list-of-floats
Python - Create a List of Floats - GeeksforGeeks
July 23, 2025 - If you want to create a list of floats based on a mathematical operation, map() combined with lambda can be efficient.
Discussions

Python 3: how to create list out of float numbers? - Stack Overflow
So, your error is that instead ... of the latitude, as a string, and adding that character to a list. result=[] for value in response_i['objcontent'][0]['rowvalues']: lat = value[0] print(lat) result.append(float(lat)) print (result) Besides that, using range(len(...))) is the way things have to be done in almost all modern languages, because they either don't implement a "for ...each" or do it in an incomplete or faulty way. In Python, since the ... More on stackoverflow.com
๐ŸŒ stackoverflow.com
python - How do I convert all of the items in a list to floats? - Stack Overflow
I have a script which reads a text ... out of it as strings and places them into a list. ... But this doesn't seem to work for me. ... Don't use list as a variable name. ... To elaborate on above comment: using list as a variable name will shadow the built-in list constructor thus not allowing you to use that in the same scope ... to be precise, it creates a new list with float ... More on stackoverflow.com
๐ŸŒ stackoverflow.com
Python creating a lists of lists of floats from a single list of strings - Stack Overflow
I'm new to python and I need to create a list of lists (a matrix) of float values from a list of strings. So if my input is: objectListData = ["1, 2, 3, 4", "5, 6, 7, 8", "9, 0, 0, 7", "5, 4, 3, 2... More on stackoverflow.com
๐ŸŒ stackoverflow.com
September 18, 2025
No way i am trying lets me convert string list into float list
Format your code More on reddit.com
๐ŸŒ r/learnpython
8
1
April 13, 2021
๐ŸŒ
Stack Overflow
stackoverflow.com โ€บ questions โ€บ 72966341 โ€บ create-a-list-of-floats-with-only-a-specific-decimal-0-0-0-7-1-0-1-7-2-0-2
python - Create a list of floats with only a specific decimal [0.0, 0.7, 1.0, 1.7, 2.0, 2.7, 3.0, 3.7, 4.0, 4.7, ...etc] - Stack Overflow
import math Units = 5.8 addition = float("0." + str(Units).split(".")[1]) intList = list(range(math.ceil(Units))) finalList = intList + [x+addition for x in intList] finalList.sort() print(finalList) ... Per funnydman and Nin17 ... the addition part could be dirty. You could change to something like the below. The assumption is that Units will always be a float (have a decimal) because we are splitting it at that decimal to find the number of decimals points.
๐ŸŒ
Sololearn
sololearn.com โ€บ en โ€บ Discuss โ€บ 1810071 โ€บ float-list-in-python
Float list in Python | Sololearn: Learn to code for FREE!
How to create a range list with float numbers: numbers = list(range(0, 10, 0.5)) pythonlists ยท 21st May 2019, 9:29 AM ยท Leon KV ยท 4 Answers ยท Answer ยท + 9 ยท # using numpy: import numpy as np l = list(np.arange(0, 10, .5)) # manually: l ...
๐ŸŒ
Python Guides
pythonguides.com โ€บ create-list-in-python
Python Lists Of Floats
July 23, 2025 - In this tutorial, I will explain an important topic known as lists of floats in Python. I will explain how to create and manipulate lists of floats in Python with examples.
๐ŸŒ
Finxter
blog.finxter.com โ€บ 5-best-ways-to-generate-list-of-floats-in-python
5 Best Ways to Generate List of Floats in Python โ€“ Be on the Right Side of Change
February 17, 2020 - from decimal import Decimal start = Decimal('0.5') end = Decimal('5.5') step = Decimal('0.5') float_list = [float(start + i * step) for i in range(int((end - start) / step))] print(float_list) ... This one-liner uses the Decimal objects to create accurately-spaced floating-point numbers.
Find elsewhere
๐ŸŒ
Stack Abuse
stackabuse.com โ€บ bytes โ€บ how-to-add-a-float-to-a-list-in-python
How to Add a Float to a List in Python
July 1, 2022 - >>> float_list = [1.0, 2.5, 3.9] >>> float_list.extend([11.2, 12.3, 13.4]) >>> float_list [1.0, 2.5, 3.9, 11.2, 12.3, 13.4] The extend() method is different from append() in that it takes an iterable of values, like a list, and adds them to the end of the target list.
๐ŸŒ
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.
Top answer
1 of 2
4

Here ya go:

[[int(y) for y in x.split(",")] for x in objectListData]

output:

[[1, 2, 3, 4], [5, 6, 7, 8], [9, 0, 0, 7], [5, 4, 3, 2], [2, 3, 3, 3], [2, 2, 3, 3]]

or, if you want floats:

[[float(y) for y in x.split(",")] for x in objectListData]

output:

[[1.0, 2.0, 3.0, 4.0], [5.0, 6.0, 7.0, 8.0], [9.0, 0.0, 0.0, 7.0], [5.0, 4.0, 3.0, 2.0], [2.0, 3.0, 3.0, 3.0], [2.0, 2.0, 3.0, 3.0]]
2 of 2
2

The problem is that your inner lists are references to one single list and not individual lists.

>>> objectListDataFloats = [[0] * len(objectListData[0].split(', '))] * len(objectListData)

>>> objectListDataFloats
[[0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]]
>>> id(objectListDataFloats[0]) == id(objectListDataFloats[1])
True

After you fix that, you need to iterate from the starting index of 0, since the lists in Python start their index from 0.

for count in range(len(objectListData)):
    for ii in range(len(objectListData[count].split(', '))):
        objectListDataFloats[count][ii] = float(objectListData[count].split(', ')[ii])


>>> objectListDataFloats
[[1.0, 2.0, 3.0, 4.0], [5.0, 6.0, 7.0, 8.0], [9.0, 0.0, 0.0, 7.0], [5.0, 4.0, 3.0, 2.0], [2.0, 3.0, 3.0, 3.0], [2.0, 2.0, 3.0, 3.0]]

To completely do away with the initial initialization of the list with zeroes, you could also just build the list as you go along, something like

>>> objectListDataFloats = []
>>> for elem in objectListData:
        test_list = []
        for val in elem.split(','):
            test_list.append(float(val))
        objectListDataFloats.append(test_list)


>>> objectListDataFloats
[[1.0, 2.0, 3.0, 4.0], [5.0, 6.0, 7.0, 8.0], [9.0, 0.0, 0.0, 7.0], [5.0, 4.0, 3.0, 2.0], [2.0, 3.0, 3.0, 3.0], [2.0, 2.0, 3.0, 3.0]]

You don't need to iterate over the list or a string by using indices, you can just iterate over the list like in the above example.

Reduced Solution -

You could just reduce the whole solution to the following though (Change int to float if you require floating point numbers)

>>> objectListData = ["1, 2, 3, 4", "5, 6, 7, 8", "9, 0, 0, 7", "5, 4, 3, 2", "2, 3, 3, 3", "2, 2, 3, 3"]
>>> [map(int, elem.split(',')) for elem in objectListData]
[[1, 2, 3, 4], [5, 6, 7, 8], [9, 0, 0, 7], [5, 4, 3, 2], [2, 3, 3, 3], [2, 2, 3, 3]]
๐ŸŒ
Quora
quora.com โ€บ How-do-you-write-a-Python-program-to-create-a-list-of-10-floating-point-numbers-and-print-all-but-the-last-4-numbers
How to write a Python program to create a list of 10 floating point numbers and print all but the last 4 numbers - Quora
Answer (1 of 3): How we can write ... it : Step1 : Firstly create a list of 10 floating point number. Step2 : Then we can use a range function the range() function returns the se......
๐ŸŒ
Python Forum
python-forum.io โ€บ thread-1743.html
How to use a list of floats
October 25, 2023 - How can make a list of floats useful if you can't iterate through them? I've tried things like: fred = ', '.join(total) fred = map(float, fred) fred = [float(x) for x in total] fred = list(map(float, total)
๐ŸŒ
GeeksforGeeks
request.geeksforgeeks.org
Python - Create a List of Floats - GeeksforGeeks
There are several methods to create a list of floats in Python, including direct definition, list comprehension, using map with lambda, and utilizing the numpy library for larger or more complex lists.
๐ŸŒ
ItSolutionstuff
itsolutionstuff.com โ€บ post โ€บ python-generate-list-of-random-float-numbers-exampleexample.html
Python Generate List of Random Float Numbers Example - ItSolutionstuff.com
December 8, 2016 - import random import numpy as np low = 0.2 high = 2.9 # Python Generate List of Random Float Numbers Example floatList = [random.uniform(low, high) for _ in range(5)] print(floatList) ... [1.2810449626587266, 1.5206898226328796, 1.9837497458591744, 0.8076086725933713, 0.6949857530454222] ... import random import numpy as np low = 0.2 high = 2.9 # Python Generate List of Random Float Numbers Example floatList = [round(random.uniform(low, high), 2) for _ in range(5)] print(floatList)
๐ŸŒ
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) ...
๐ŸŒ
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
April 13, 2021 -

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)

๐ŸŒ
Finxter
blog.finxter.com โ€บ 5-best-ways-to-create-a-python-list-of-floats-with-step
5 Best Ways to Create a Python List of Floats with Step โ€“ Be on the Right Side of Change
April 9, 2024 - This code snippet utilizes the numpy library to create a list of floats starting at 0.5 and ending at 5.5 with a 0.5 step. The result is an ndarray which we convert to a list using the tolist method. This is a compact and efficient approach for working with numerical sequences in Python.
๐ŸŒ
Reddit
reddit.com โ€บ r/learnpython โ€บ help generating a list of random floats that will add up to a specific value.
r/learnpython on Reddit: Help generating a list of random floats that will add up to a specific value.
March 14, 2025 -

Trying to create a function that takes a specified length for a list, a range from a negative to positive number, and a sum. For example random_list_sum_generator(length=5, float_range=(-3.0, 3.0), sum=0) should generate a list of 5 random floats within the range -3. to 3. that cumulatively sum to 0. I have been unable to do it thus far.

Chatgpt wants to randomly generate the first 4 numbers, and then calculate the difference and then set the last number. The problem with that is that the last number might then be outside of the specified float_range.

How can I go about doing this, it doesn't seem like it would be to hard conceptually but the attempts have been unfruitful so far.

Top answer
1 of 4
3
I'm working through the problem as I type this, maybe my thoughts will help, but this is probably going to be a bit of a train-of-thought mess. My first thought is that this is more a math problem than a programming one (or at least it's math problem first and then a programming problem). What does it even mean to generate 5 numbers that sum to a given sum? Such a list only has 4 degrees of freedom. I'm gonna simplify the problem to make it easier to get a handle on: how would I generate just 2 numbers that have a given sum? Let's call the first number x and the second y, then the set of all possible pairs that have that sum would look like a line in the xy plane. The fact that each coordinate has to be in a certain range restricts us to inside a box in the plane, leaving us with only a line segment of valid points. So it turns out to be pretty easy in this case, you can just pick any x and calculate the corresponding y. Let's bump it up to 3d. I want an x, y, and z that are each within a certain range (meaning the point (x,y,z) lies within a certain cube if I were to graph it) and their sum lies on a certain plane. Takes some thinking, but the cross section of that plane in the cube is a hexagon. Picking a random point inside a hexagon isn't a trivial thing, you have to choose a probability distribution out of infinitely many options (technically this is true for even a square, it's just that there the symetry of the shape points you to a particular choice of distribution so strongly that it's not even obvious you're making a choice) and the shape is only going to get more complex as you go up in dimensions. So this is a Hard Problem in general, and you're going to have to make some tough choices about what kind of properties you want this function to have. One natural requirement is that order not matter, i.e. the points (x_1,y_1,z_1), (z_1,y_1,x_1) and (y_1,x_1,z_1) are all equally likely, but that requirement is probably pretty hard to pull off, computationally speaking.
2 of 4
3
Generate 5 random numbers without caring about the sum, and then rescale all of them so they add up to the right value. Edited to add: If the rescaled numbers violate the per-number range constraint, discard the set and try again. Edited further to add: Actually, between a scale and an offset, you can always constrain the numbers to sum to the right value and fit within the range. Edit3: "Always" caveat. Provided the conditions for the set aren't inconsistent. eg. N= 2, Min=-1, Max=1, Sum=15 is simply not possible to achieve