2

prorgamming newbie--I was looking for answers to an exercise I was doing and got my answers from here. My question is this--from that thread, the one chosen as best answer, was this code

[float(i) for i in lst]

The code did what it was supposed to do, but when I tried to get to that new list, I am getting errors

>>> xs = '12 10 32 3 66 17 42 99 20'.split()
>>> [float(i) for i in xs]
[12.0, 10.0, 32.0, 3.0, 66.0, 17.0, 42.0, 99.0, 20.0]
>>> i
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'i' is not defined  

How should I do it?

Thanks!

Community
  • 1
  • 1
Jim Syyap
  • 1,625
  • 7
  • 20
  • 23
  • 3
    Whenever the python prompt prints something out, it's because whatever you just typed returned a value. You can keep that value by assigning a variable to the expression you typed, but then nothing will be printed out. – SingleNegationElimination May 15 '11 at 22:57

6 Answers6

7

You have to assign [float(i) for i in xs] to something:

>>> new_list = [float(i) for i in xs]
>>> new_list
[12.0, 10.0, 32.0, 3.0, 66.0, 17.0, 42.0, 99.0, 20.0]
>>> new_list[0]
12.0
>>> new_list[5]
17.0
hyperslug
  • 3,473
  • 1
  • 28
  • 29
3

Using map:

xs = '12 10 32 3 66 17 42 99 20'.split()
new_xs = map(float, xs)
riza
  • 16,274
  • 7
  • 29
  • 29
  • 2
    Using [`map()`](http://docs.python.org/library/functions.html#map) is certainly the neatest way to achieve this. – johnsyweb May 16 '11 at 02:09
1

Unlike for loops that leave the last iteration bound to the variable the var inside a list comp stops existing after evaluation.

Jakob Bowyer
  • 33,878
  • 8
  • 76
  • 91
0

i only exists inside the list comprehension.

You want to say:

i = [float(i) for i in lst]
mdonoughe
  • 515
  • 5
  • 12
  • 2
    Using `i` as a variable inside and outside of the list comprehension could lead to unnecessary confusion. – johnsyweb May 15 '11 at 22:56
  • or rather, maybe what you really *want* to say is `floatlist = [....`, since naming the list after the iterator is a bit confusing. – SingleNegationElimination May 15 '11 at 22:58
  • 1
    for completeness, `i` only exists inside the list comprehension in Python 3. In Python 2, you can access `i` outside. – carl May 15 '11 at 23:03
0

This is something related to Python versions. Look: On Python 2, if you do:

>>> i = 1234
>>> j = [i for i in xrange(10)]
>>> print i
9

But this was fixed in Python 3:

>>> i = 1234
>>> j = [i for i in range(10)]
>>> print(i)
1234

If you have Python 3.0 or superior, the variable will only be available in the comprehension. It will not affect the rest of the environment

JBernardo
  • 32,262
  • 10
  • 90
  • 115
0

As an alternative to assigning a name to your list comprehension, in Python interactive mode, the symbol _ is automatically assigned to the last expression evaluated by the interpreter. So you could do the following:

>>> xs = '12 10 32 3 66 17 42 99 20'.split()
>>> [float(i) for i in xs]
[12.0, 10.0, 32.0, 3.0, 66.0, 17.0, 42.0, 99.0, 20.0]
>>> _
[12.0, 10.0, 32.0, 3.0, 66.0, 17.0, 42.0, 99.0, 20.0]
>>> _[2]
32.0
>>> _
32.0
Don O'Donnell
  • 4,538
  • 3
  • 26
  • 27