### Thread: A list comprehension problem

1. #### A list comprehension problem

I am using Python 2.3 and the round() function doesn't seem to work properly within a list comprehension statement. Any idea why this happens?
Code:
```"""
list pi values progressively rounded off using a list comprehension
this is supposed to give:
['3.1', '3.14', '3.142', '3.1416', '3.14159']
but gives:
['3.1000000000000001', '3.1400000000000001', '3.1419999999999999',
'3.1415999999999999', '3.1415899999999999']
"""
piList = [repr(round(355/113.0, k)) for k in range(1,6)]
print piList

# this rounds off correctly, result = 3.14159
print round(355/113.0, 5)```
2. No Profile Picture
Contributing User
Devshed Newbie (0 - 499 posts)

Join Date
Nov 2004
Posts
394
Rep Power
55
Hi!

The round function works as it should. What confuses you is repr. This example should make it clear:
Code:
```>>> repr(3.14)
'3.14000000000000001'```
Thats how a float is "represented". What gives the result you want is str() (I guess ):
Code:
`piList = [str(round(355/113.0, k)) for k in range(1,6)]`
Hope this helps.

Regards, mawe

• netytan agrees

3. Sorry, I am new to this!

When I use str() I get ...
TypeError: 'str' object is not callable

4. Couldn't get str() to work so I removed repr() and now have:
Code:
```"""
list pi values progressively rounded off using a list comprehension
this is supposed to give:
['3.1', '3.14', '3.142', '3.1416', '3.14159']
but gives:
[3.1000000000000001, 3.1400000000000001, 3.1419999999999999,
3.1415999999999999, 3.1415899999999999]
"""
piList = [round(355/113.0, k) for k in range(1,6)]
print piList

# this rounds off correctly, result = 3.14159
print round(355/113.0, 5)```
Notice that it still gives a goofy result. Any help would be nice!
5. No Profile Picture
Contributing User
Devshed Newbie (0 - 499 posts)

Join Date
Feb 2005
Posts
78
Rep Power
13
Originally Posted by Dietrich
Couldn't get str() to work so I removed repr() and now have:
Code:
```"""
list pi values progressively rounded off using a list comprehension
this is supposed to give:
['3.1', '3.14', '3.142', '3.1416', '3.14159']
but gives:
[3.1000000000000001, 3.1400000000000001, 3.1419999999999999,
3.1415999999999999, 3.1415899999999999]
"""
piList = [round(355/113.0, k) for k in range(1,6)]
print piList

# this rounds off correctly, result = 3.14159
print round(355/113.0, 5)```
Notice that it still gives a goofy result. Any help would be nice!
print myobj is equivalent to print str(myobj)
print mylist is equivalent to print str(mylist) which is equivalent to print str([repr(elem) for elem in mylist])

Code:
```class Foo(object):
def __str__(self):
return "STR"
def __repr__(self):
return "REPR"

print Foo()
print [Foo()]```
As mawe has said, you need to use:
Code:
`piList = [str(round(355/113.0, k)) for k in range(1,6)]`
--OH.

6. I finally caught the troublemaker!

I had looked at a series of list comprehensions and in the previous example had used str as a variable name, so the Python shell did not allow me to use str().

Works fine now, thanks experts!