Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating a list using a loop, filling it with float()

I'm currently working on a project that requires me to have a list from 0.9 to 1.2 with steps of 0.01. I tried the following:

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

However, python gives me the following output:

Traceback (most recent call last): File "", line 2, in IndexError: list assignment index out of range

Can anyone help me solve this problem?

like image 493
Yaga Avatar asked Nov 28 '13 08:11

Yaga


People also ask

How do you create a list in a for loop?

You can use a for loop to create a list of elements in three steps: Instantiate an empty list. Loop over an iterable or range of elements. Append each element to the end of the list.

How do you make a float list?

This basic method to convert a list of strings to a list of floats uses three steps: Create an empty list with floats = [] . Iterate over each string element using a for loop such as for element in list . Convert the string to a float using float(element) and append it to the new float list using the list.

Can you have a list of floats in Python?

Python is a very flexible language and allows you to add just about any type of data to a list, regardless of the data type of any of the other elements in the list. One common data type to add to a list is the float data type.


3 Answers

[] 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.

like image 108
e-satis Avatar answered Sep 28 '22 05:09

e-satis


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))]
like image 40
Fredrik Pihl Avatar answered Sep 28 '22 04:09

Fredrik Pihl


You need to extend the list. Use:

l.append(init + (float(i)/100))

or

l += [init + (float(i)/100)]

or even the more pythonic way

l = [init + (float(i)/100) for i in range(0, 31)]
like image 40
Sorin Avatar answered Sep 28 '22 05:09

Sorin