Dave Angel wrote:
Jojo Mwebaze wrote:
Hi There,

i would like to implement the following in lists

assuming

x = 3
y = 4
z = None

i want to create a dynamic list such that

mylist = [ x , y, z ] ,   if z in not None

if z is None then

mylist = [x,y]

Anyhelp!

cheers

Jojo


Are there any constraints on x and y ? If you want to throw out all None values, then it's a ready problem. You try it, and if it doesn't quite work, post the code. We'll try to help.

But if only the third value is special, then there's little point in making a comprehension of one value. Just conditionally append the z value to the list containing x and y.

DaveA

_______________________________________________
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor

Hello,

I wrote a solution which isn't correct.

>>> [i for i in mylist if i]
[3, 4]


The if condition should test like the other examples given to the list.

>>> [i for i in mylist if i != None]
[3, 4]

'if i' would also leave out the integer 0.

T
_______________________________________________
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor

Reply via email to