Fix Python – Accessing class variables from a list comprehension in the class definition

How do you access other class variables from a list comprehension within the class definition? The following works in Python 2 but fails in Python 3:
class Foo:
x = 5
y = [x for i in range(1)]

Python 3.2 gives the error:
NameError: global name ‘x’ is not defined

Trying Foo.x doesn’t work either. Any ideas on how to do this in Python 3?….

Fix Python – Python’s most efficient way to choose longest string in list?

I have a list of variable length and am trying to find a way to test if the list item currently being evaluated is the longest string contained in the list. And I am using Python 2.6.1
For example:
mylist = [‘abc’,’abcdef’,’abcd’]

for each in mylist:
if condition1:
do_something()
elif ___________________: #else if each is the long….

Fix Python – List comprehension on a nested list?

I have this nested list:
l = [[’40’, ’20’, ’10’, ’30’], [’20’, ’20’, ’20’, ’20’, ’20’, ’30’, ’20’], [’30’, ’20’, ’30’, ’50’, ’10’, ’30’, ’20’, ’20’, ’20’], [‘100’, ‘100’], [‘100’, ‘100’, ‘100’, ‘100’, ‘100’], [‘100’, ‘100’, ‘100’, ‘100’]]

Now, what I want to do is convert each element in a list to float. My solution is this:
newList = []
for x in….

Fix Python – remove None value from a list without removing the 0 value

This was my source I started with.
My List
L = [0, 23, 234, 89, None, 0, 35, 9]

When I run this :
L = filter(None, L)

I get this results
[23, 234, 89, 35, 9]

But this is not what I need, what I really need is :
[0, 23, 234, 89, 0, 35, 9]

Because I’m calculating percentile of the data and the 0 make a lot of difference.
How to remove the None….

Fix Python – Double Iteration in List Comprehension

In Python you can have multiple iterators in a list comprehension, like
[(x,y) for x in a for y in b]

for some suitable sequences a and b. I’m aware of the nested loop semantics of Python’s list comprehensions.
My question is: Can one iterator in the comprehension refer to the other? In other words: Could I have something like this:
[x for x in a….

Fix Python – Why is there no tuple comprehension in Python?

As we all know, there’s list comprehension, like
[i for i in [1, 2, 3, 4]]

and there is dictionary comprehension, like
{i:j for i, j in {1: ‘a’, 2: ‘b’}.items()}

but
(i for i in (1, 2, 3))

will end up in a generator, not a tuple comprehension. Why is that?
My guess is that a tuple is immutable, but this does not seem to be the answer.
….