Add elements to a list within a list comprehension

1

I have following code:

myList = [1,2,3,4,5]
result = []
test = [result.append(i) for i in myList]

Whose output of test is:

  

[None, None, None, None, None]

I would like to know why, since for example:

test = [print(i) for i in myList]

Print each one successfully

I do not want the solution to this, I know in a normal for loop this results, I would just like to know why append is not executed as I want

    
asked by anonymous 25.05.2016 / 19:51

2 answers

2

Because you are adding in the understanding the return of the 'append' function and the function returns None. Example:

>>> print(result.append(i))
None

But the 'result' variable got the values 'appended':

>>> print(result)
[1, 2, 3, 4, 5]
    
25.05.2016 / 20:44
0

Take a look at this code, I believe it solves your question. :)

data = ['3', '7.4', '8.2']

new_data = [float(n) for n in data]

new_data

[3.0, 7.4, 8.2]
    
25.10.2017 / 20:47