[] works only if there is an element already at this index the list. Use list.append():

init = float(0.9)
l = []
for i  in range(0,31):
  l.append(init + ( float(i) / 100 ))

Once you are confortable with this, you can even use a comprehension list :

l = [init + (float(i) / 100 )) for i in range(0, 31)]

It is very rare in Python to use indexes. I understand a lot people do it because it's an habit in other languages, but most of the time, it's an anti-pattern in Python. If you you see a i somewhere, always wonder if you are not trying to reinvent the wheel.

BTW. Division has priority on addition so no need for parenthesis. Plus, init = float(0.9) is redundant. you can write init = 0.9. And the / always returns a float, therefor you can do :

l = []
for i in range(0, 31): 
  l.append(0.9 + i / 100)

Also note the way I place spaces. It's the most used style convention in Python.

And with a comprehension list :

l = [0.9 + i / 100 for i in range(0, 31)]

It is a much simpler way to achieve what you want. Don't worry, if your code works and you understand it, it's the most important. You don't NEED to do this. I'm just giving you this information so you can use it later if you wish.

Answer from Bite code on Stack Overflow
Top answer
1 of 9
14

[] works only if there is an element already at this index the list. Use list.append():

init = float(0.9)
l = []
for i  in range(0,31):
  l.append(init + ( float(i) / 100 ))

Once you are confortable with this, you can even use a comprehension list :

l = [init + (float(i) / 100 )) for i in range(0, 31)]

It is very rare in Python to use indexes. I understand a lot people do it because it's an habit in other languages, but most of the time, it's an anti-pattern in Python. If you you see a i somewhere, always wonder if you are not trying to reinvent the wheel.

BTW. Division has priority on addition so no need for parenthesis. Plus, init = float(0.9) is redundant. you can write init = 0.9. And the / always returns a float, therefor you can do :

l = []
for i in range(0, 31): 
  l.append(0.9 + i / 100)

Also note the way I place spaces. It's the most used style convention in Python.

And with a comprehension list :

l = [0.9 + i / 100 for i in range(0, 31)]

It is a much simpler way to achieve what you want. Don't worry, if your code works and you understand it, it's the most important. You don't NEED to do this. I'm just giving you this information so you can use it later if you wish.

2 of 9
3

Use the itertools module:

In [8]: for i in itertools.count(0.9, 0.01):
   ...:    print i
   ...:    if i > 1.2:
   ...:        break
   ...:     
0.9
0.91
0.92
0.93
.
.
.
1.18
1.19
1.2

As pointed out in the comments, the count() can be combined with a takewhile in order to create a list-comprehension:

[i for i in itertools.takewhile(lambda x: x <= 1.2, itertools.count(0.9, 0.01))]
🌐
Stack Overflow
stackoverflow.com › questions › 60348228 › python-filling-array-in-the-loop
Python. Filling array in the loop - Stack Overflow
February 22, 2020 - I need to create 2d array and fill it in the loop. I know how many rows I need, but I don't know what size each row will be after the loop. In C++ I can use: vector<vector<int>> my_vec(3); my_vec[0].push_back(1); my_vec[0].push_back(2); my_vec[1].push_back(3); my_vec[2].push_back(4); ... I think it's how python creates array, all cells reference to the same memory spot.
Discussions

filling an array with a loop in python - Stack Overflow
Communities for your favorite technologies. Explore all Collectives · Stack Overflow for Teams is now called Stack Internal. Bring the best of human thought and AI automation together at your work More on stackoverflow.com
🌐 stackoverflow.com
May 22, 2017
filling an array with a loop in python - Stack Overflow
Communities for your favorite technologies. Explore all Collectives · Turn scattered docs and code into trusted AI context. Explore the new Stack Internal platform More on stackoverflow.com
🌐 stackoverflow.com
python - fill an array line by line in a loop - Stack Overflow
Suppose I have a loop that creates an array, line by line, starting from an empty array. More on stackoverflow.com
🌐 stackoverflow.com
September 30, 2021
fill empty array using loop in python - Stack Overflow
i'm new to python and i'm asked to make a basic calculator using 3 input int, int, str. the input and output should be like this: INPUT 1 2 ADD 4 100 MUL 5 2 DIV 100 10 SUB OUTPUT 3 400 2 90 Here's More on stackoverflow.com
🌐 stackoverflow.com
September 28, 2018
Top answer
1 of 2
1

You don't really need to fill the array. np.linspace generates a uniformly spaced numpy array for you. However, given your requirement you can do this:

Solution 1: Loop method

import numpy as np
from numpy import log

#filling zeros
x_array = np.zeros(101)
y_array = np.zeros(101)

#generating uniformly spaced numbers
x=np.linspace(1,10,101)

#filling the array with loop
for i in range(0,len(x)):
    x_array[i] = x[i] #filling  x_array
    y_array[i] = log(x[i]) #filling y_array

print(x_array,y_array)

Solution 2: List comprehension method

import numpy as np
from numpy import log

#filling zeros
x_array = np.zeros(101)
y_array = np.zeros(101)

#generating uniformly spaced numbers
x=np.linspace(1,10,101)

#filling the array with list comprehension
x_array = [ x[i] for i in range(0,len(x))]
y_array = [ log(x[i]) for i in range(0,len(x))]

print(len(x_array),len(y_array))
print(x_array,y_array)

Output:

#length
101 101

#x_array
[1.0, 1.09, 1.18, 1.27, 1.3599999999999999, 1.45, 1.54, 1.63, 1.72, 1.81, 1.9, 1.99, 2.08, 2.17, 2.26, 2.3499999999999996, 2.44, 2.5300000000000002, 2.62, 2.71, 2.8, 2.8899999999999997, 2.98, 3.07, 3.16, 3.25, 3.34, 3.4299999999999997, 3.52, 3.61, 3.6999999999999997, 3.79, 3.88, 3.9699999999999998, 4.0600000000000005, 4.15, 4.24, 4.33, 4.42, 4.51, 4.6, 4.6899999999999995, 4.779999999999999, 4.869999999999999, 4.96, 5.05, 5.14, 5.2299999999999995, 5.32, 5.41, 5.5, 5.59, 5.68, 5.77, 5.859999999999999, 5.95, 6.04, 6.13, 6.22, 6.31, 6.3999999999999995, 6.49, 6.58, 6.67, 6.76, 6.85, 6.9399999999999995, 7.029999999999999, 7.12, 7.21, 7.3, 7.39, 7.4799999999999995, 7.569999999999999, 7.66, 7.75, 7.84, 7.93, 8.02, 8.11, 8.2, 8.29, 8.379999999999999, 8.469999999999999, 8.559999999999999, 8.649999999999999, 8.739999999999998, 8.83, 8.92, 9.01, 9.1, 9.19, 9.28, 9.37, 9.459999999999999, 9.549999999999999, 9.64, 9.73, 9.82, 9.91, 10.0] 

#y_erray
[0.0, 0.08617769624105241, 0.16551443847757333, 0.23901690047049992, 0.30748469974796055, 0.371563556432483, 0.43178241642553783, 0.4885800148186709, 0.5423242908253617, 0.5933268452777344, 0.6418538861723947, 0.688134638736401, 0.7323678937132266, 0.7747271675523681, 0.8153648132841944, 0.8544153281560675, 0.8919980393051105, 0.928219302739429, 0.9631743177730056, 0.9969486348916096, 1.0296194171811581, 1.0612565021243408, 1.091923300517313, 1.1216775615991057, 1.1505720275988207, 1.1786549963416462, 1.205970806988609, 1.2325602611778486, 1.2584609896100056, 1.2837077723447896, 1.3083328196501787, 1.332366019094335, 1.355835153635182, 1.378766094699099, 1.4011829736136414, 1.423108334242607, 1.4445632692438664, 1.4655675420143985, 1.4861396960896067, 1.506297153514587, 1.5260563034950492, 1.5454325824581878, 1.5644405465033644, 1.5830939370944983, 1.601405740736836, 1.6193882432872684, 1.6370530794670737, 1.6544112780768314, 1.6714733033535532, 1.6882490928583902, 1.7047480922384253, 1.7209792871670078, 1.7369512327330598, 1.7526720805200082, 1.7681496035889213, 1.7833912195575383, 1.7984040119467235, 1.81319474994812, 1.827769906751088, 1.8421356765531218, 1.856297990365626, 1.8702625307159986, 1.884034745337226, 1.8976198599275322, 1.9110228900548727, 1.9242486522741338, 1.937301774518713, 1.9501867058225735, 1.9629077254238845, 1.9754689512968577, 1.9878743481543455, 2.0001277349601105, 2.012232791986386, 2.024193067449357, 2.0360119837525, 2.0476928433652555, 2.0592388343623163, 2.0706530356467567, 2.081938421878423, 2.0930978681273213, 2.1041341542702074, 2.1150499691472033, 2.1258479144939915, 2.136530508663963, 2.1471001901536506, 2.157559320943788, 2.167910189667444, 2.178155014615869, 2.188295946591918, 2.1983350716202463, 2.2082744135228043, 2.2181159363675955, 2.2278615467981093, 2.2375130962503307, 2.2470723830637866, 2.2565411544926386, 2.265921108622454, 2.2752138961979136, 2.2844211223663744, 2.2935443483418965, 2.302585092994046]

EDIT:

Adding the plotting functionality:

#add this code below the above code if you want a plot
import matplotlib.pyplot as plt

plt.plot(x_array,y_array)
plt.title(r'')
plt.show()

Output:

2 of 2
0

You can generate x_array using range() and then fill y_array accordingly using math.log():

from math import log

x_array = list(range(1, 11))
y_array = []
for val in x_array:
    y_array.append(log(val))

print(x_array)
print(y_array)

Or, a bit concisely, use list-comprehension:

from math import log

x_array = list(range(1, 11))
y_array = [log(val) for val in x_array]

print(x_array)
print(y_array)
🌐
Tudelft
nsweb.tn.tudelft.nl › ~gsteele › TN2513_Tips › Creating a numpy array in a loop.html
Creating a numpy array in a loop
Numpy also has an append function, but it does not append to a given array, it instead creates a new array with the results appended. This is both memory inefficient, and also computationally inefficient. You should only use np.append if you really want a second copy of the array. Here below though is how you would use np.append in the loop:
Find elsewhere
🌐
Delft Stack
delftstack.com › home › howto › numpy › python fill array with value
How to Fill Array With Value in NumPy | Delft Stack
March 11, 2025 - The numpy.full() function is ideal for creating new arrays, while the numpy.fill() method is perfect for modifying existing ones. If you need more control or wish to implement custom logic, using a for loop is also an option.
🌐
Stack Overflow
stackoverflow.com › questions › 52546411 › fill-empty-array-using-loop-in-python
fill empty array using loop in python - Stack Overflow
September 28, 2018 - In Python, you cannot enlarge a list by assigning to nonexistent indexes - you have to use the .append() or .extend() methods.
Top answer
1 of 4
2

Your code at the moment seems very hard coded I'm unsure as to what you are trying to achieve but try:

DU = []

for index, line in enumerate(oz):

   line = line.strip() if (index +1) % 15 != 0 else line.strip().split(' ')[0]

   for i in range(0,len(line)-3,3):

      DU.append(line[i:i+3])

or could you try a combination of the answers

 from itertools import izip

 def grouped(iterable, n):
      "s -> (s0,s1,s2,...sn-1), (sn,sn+1,sn+2,...s2n-1), (s2n,s2n+1,s2n+2,...s3n-1), ..."
      return izip(*[iter(iterable)]*n)

 DU = []

 for index, line in enumerate(oz):

        line = line.strip() if (index +1) % 15 != 0 else line.strip().split(' ')[0]


        DU.append(map(''.join, grouped(line.strip(), 3)))
2 of 4
1

You might be able to use something along the lines of the update in my answer to another question here to group the string of digits in each number. Specifically using this code with the number string as the iterable and a value of 3 for the n (group-size) argument:

from itertools import izip

def grouped(iterable, n):
    "s -> (s0,s1,s2,...sn-1), (sn,sn+1,sn+2,...s2n-1), (s2n,s2n+1,s2n+2,...s3n-1), ..."
    return izip(*[iter(iterable)]*n)

digits = '283283283283283283283283284284284284284284284284284284284284284284284284284\n'

print map(''.join, grouped(digits.strip(), 3))

Output:

['283', '283', '283', '283', '283', '283', '283', '283', '284', '284', 
'284', '284', '284', '284', '284', '284', '284', '284', '284', '284', 
'284', '284', '284', '284', '284']

I note however that the last line of the data in your example, the:

'294294294294294294294294295295 lat = -89.5\n'

is not simply a string of digits, so it will have to be handled as a special case.

Update:

OK, now that I see the additional information you added to your question, I can provide a complete solution for you based on the grouped() function from my other answer, as I initially suggested. This handles the special line which occurs periodically in your input data by splitting up each line of data and then ignoring all but the first -- often the only -- item of that, which is always a string of digits which are then further processed via my function.

from itertools import izip

def grouped(iterable, n):
    "s -> (s0,s1,s2,...sn-1), (sn,sn+1,sn+2,...s2n-1), (s2n,s2n+1,s2n+2,...s3n-1), ..."
    return izip(*[iter(iterable)]*n)

data = [' 283283283283283283283283284284284284284284284284284284284284284284284284284\n', ' 284284284284284284284284284284284284284284284284284284284284284284284284284\n', ' 284284284284284284284284284284284284284284284284284284284284284284284284284\n', ' 284284284284284284284284284284284284284284284284284284284284284284284284284\n', ' 284284284284284284284284284284284284284284284284284284284284284284283283283\n', ' 283283283283283283283283283283283283283283283283283283283283283283283283283\n', ' 283283283283283283283283283283283283283283283283283283283283283283283283283\n', ' 283283283283283283283283283283283283283283283283283283283283283283283283283\n', ' 283283283283283282282283283282282282282283283283283283283283283283283283283\n', ' 283283283283283283283283283283283283283283283283283283283283283284284284284\n', ' 284284284284284284284284284284284285285285285285285285285285285285285285285\n', ' 285285286286286286286286286287287287287287287288288288288288288288288288288\n', ' 288289289289289289289289289290290290290290290290290290291291291291291291291\n', ' 291291292292292292292292293293293293293293293293293293293293294294294294294\n', ' 294294294294294294294294295295   lat =  -89.5\n', ' 284284284284284284284284284284284284284284284284284284284284284284284284284\n', ' 284284284284284284284284284284283283284284284284284284284284284284284284283\n', ' 283283283283283283283283283283283283283283283283283283283283283283283283283\n', ' 283283283283283283283283283283284284284284284284284284284284284284284284284\n', ' 284284284284284284284284284284284284283283283283283283283283283283283283283\n', ' 283283283283283283283283283283283282282282282282282282282282282282282282281\n', ' 281281281281281281281281281281281281281281281281280280280280280280280280279\n', ' 279279279279279279279279279279279279279279278278278278278278278278278278278\n', ' 277277278278278278278278278278278278278278278278278278278278278278278278278\n', ' 278278279279279279279279279279279279279279279279279279279279279279279279279\n', ' 279279280280280280280280280280280280280280280280280280280281281281281281281\n', ' 281282282282282282282282283283283283283283284284284284284284285285285285285\n', ' 286286286287287287287288288288288288288289289289289289290290290290291291291\n', ' 292292292292292292293293293293293293293293293294294294294295295295295295295\n', ' 296296296296296296296297297297   lat =  -88.5\n']

DU = []
for line in data:
    DU.extend(map(''.join, grouped(line.strip().split()[0], 3)))

print DU

Output:

['283', '283', '283', '283', '283', '283', '283', '283', '284', '284', '284', '284', '284', '284', '284', '284', '284', '284', '284', '284', '284', '284', '284', '284', '284', '284', '284', '284', '284', '284', '284', '284', '284', '284', '284', '284', '284', '284', '284', '284', '284', '284', '284', '284', '284', '284', '284', '284', '284', '284', '284', '284', '284', '284', '284', '284', '284', '284', '284', '284', '284', '284', '284', '284', '284', '284', '284', '284', '284', '284', '284', '284', '284', '284', '284', '284', '284', '284', '284', '284', '284', '284', '284', '284', '284', '284', '284', '284', '284', '284', '284', '284', '284', '284', '284', '284', '284', '284', '284', '284', '284', '284', '284', '284', '284', '284', '284', '284', '284', '284', '284', '284', '284', '284', '284', '284', '284', '284', '284', '284', '284', '284', '283', '283', '283', '283', '283', '283', '283', '283', '283', '283', '283', '283', '283', '283', '283', '283', '283', '283', '283', '283', '283', '283', '283', '283', '283', '283', '283', '283', '283', '283', '283', '283', '283', '283', '283', '283', '283', '283', '283', '283', '283', '283', '283', '283', '283', '283', '283', '283', '283', '283', '283', '283', '283', '283', '283', '283', '283', '283', '283', '283', '283', '283', '283', '283', '283', '283', '283', '283', '283', '283', '283', '283', '283', '283', '283', '283', '283', '283', '283', '283', '283', '283', '283', '282', '282', '283', '283', '282', '282', '282', '282', '283', '283', '283', '283', '283', '283', '283', '283', '283', '283', '283', '283', '283', '283', '283', '283', '283', '283', '283', '283', '283', '283', '283', '283', '283', '283', '283', '283', '283', '283', '283', '283', '283', '284', '284', '284', '284', '284', '284', '284', '284', '284', '284', '284', '284', '284', '284', '284', '285', '285', '285', '285', '285', '285', '285', '285', '285', '285', '285', '285', '285', '285', '285', '285', '286', '286', '286', '286', '286', '286', '286', '287', '287', '287', '287', '287', '287', '288', '288', '288', '288', '288', '288', '288', '288', '288', '288', '288', '289', '289', '289', '289', '289', '289', '289', '289', '290', '290', '290', '290', '290', '290', '290', '290', '290', '291', '291', '291', '291', '291', '291', '291', '291', '291', '292', '292', '292', '292', '292', '292', '293', '293', '293', '293', '293', '293', '293', '293', '293', '293', '293', '293', '294', '294', '294', '294', '294', '294', '294', '294', '294', '294', '294', '294', '294', '295', '295', '284', '284', '284', '284', '284', '284', '284', '284', '284', '284', '284', '284', '284', '284', '284', '284', '284', '284', '284', '284', '284', '284', '284', '284', '284', '284', '284', '284', '284', '284', '284', '284', '284', '284', '284', '283', '283', '284', '284', '284', '284', '284', '284', '284', '284', '284', '284', '284', '284', '283', '283', '283', '283', '283', '283', '283', '283', '283', '283', '283', '283', '283', '283', '283', '283', '283', '283', '283', '283', '283', '283', '283', '283', '283', '283', '283', '283', '283', '283', '283', '283', '283', '283', '283', '283', '284', '284', '284', '284', '284', '284', '284', '284', '284', '284', '284', '284', '284', '284', '284', '284', '284', '284', '284', '284', '284', '284', '284', '284', '284', '284', '284', '283', '283', '283', '283', '283', '283', '283', '283', '283', '283', '283', '283', '283', '283', '283', '283', '283', '283', '283', '283', '283', '283', '283', '283', '282', '282', '282', '282', '282', '282', '282', '282', '282', '282', '282', '282', '282', '281', '281', '281', '281', '281', '281', '281', '281', '281', '281', '281', '281', '281', '281', '281', '281', '281', '280', '280', '280', '280', '280', '280', '280', '280', '279', '279', '279', '279', '279', '279', '279', '279', '279', '279', '279', '279', '279', '279', '279', '278', '278', '278', '278', '278', '278', '278', '278', '278', '278', '278', '277', '277', '278', '278', '278', '278', '278', '278', '278', '278', '278', '278', '278', '278', '278', '278', '278', '278', '278', '278', '278', '278', '278', '278', '278', '278', '278', '279', '279', '279', '279', '279', '279', '279', '279', '279', '279', '279', '279', '279', '279', '279', '279', '279', '279', '279', '279', '279', '279', '279', '279', '279', '280', '280', '280', '280', '280', '280', '280', '280', '280', '280', '280', '280', '280', '280', '280', '280', '280', '281', '281', '281', '281', '281', '281', '281', '282', '282', '282', '282', '282', '282', '282', '283', '283', '283', '283', '283', '283', '284', '284', '284', '284', '284', '284', '285', '285', '285', '285', '285', '286', '286', '286', '287', '287', '287', '287', '288', '288', '288', '288', '288', '288', '289', '289', '289', '289', '289', '290', '290', '290', '290', '291', '291', '291', '292', '292', '292', '292', '292', '292', '293', '293', '293', '293', '293', '293', '293', '293', '293', '294', '294', '294', '294', '295', '295', '295', '295', '295', '295', '296', '296', '296', '296', '296', '296', '296', '297', '297', '297']

You could make an efficient fairly unreadable list comprehension out of it like this:

from itertools import chain

DU = list(chain.from_iterable(map(''.join, grouped(line.strip().split()[0], 3))
                                             for line in data))
🌐
Real Python
realpython.com › videos › append-in-lists-for-loops
Populating Lists From Scratch in "for" Loops (Video) – Real Python
In this lesson, you’ll see one of the most common uses of the .append() method, which is populating a list from within a loop. A very common task in Python—and other programming languages, for that matter—is to create a list iteratively.
Published: November 16, 2021
🌐
Reddit
reddit.com › r/learnpython › how to fill a matrix using numpy
r/learnpython on Reddit: How to fill a matrix using numpy
June 18, 2020 -

Hello everyone,

I haven't done any type of linear algebra work with python before, only matlab. In matlab you create a matrix with the proper dimensions, then start filling it up with vectors of the appropriate size. I've tried to look up how to do this in python using a loop, and it doesn't seem as straightforward. It seems you first need to create a list, and append that list. Then convert that list to a numpy array. And then from there it appears you stack them? But stacking seems to only stack 1D and 2D arrays, so it's not exactly what I'm looking for.

For example:

import numpy as np
matrix=np.zeros((rows,columns))
list=[]
for i in range(columns):
    for x in range(rows):
        list.append(x)
    arr=np.array(list)
    matrix.append(arr) 
    list.clear()   

So in the above case, I've made a list, and converted it into an array with shape (rows, 1). Then I want to add it to the first column of my matrix. Then through the loop, I start to fill my matrix up by filling it one by one with these vectors. However, the issue is I'm trying to add a 1D array, to a multi-D matrix (which numpy doesn't allow you to do).

Hpw do I fill up a matrix using numpy?

🌐
W3Schools
w3schools.com › python › gloss_python_array_loop.asp
Python Loop Through an Array
You can use the for in loop to loop through all the elements of an array. ... Python Array Tutorial Array What is an Array Access Arrays Array Length Add Array Element Remove Array Element Array Methods