Tuchar Das
Tuchar Das

Reputation: 53

shorthand if else in list comprehension

This is my code for making half and double of even and odd numbers in a list. I know that it will work with block for loop. But i want to know why list comprehension didn't work Thanks in Advance 😀

entry = input('Enter a number list ').split()

#Convert all items to integer
data = [int(x) for x in entry]

print(data)

rslt = [(a/2 if a%2==0 else 2*a) for a in data]

print(data)

OUTPUT :

Enter a number list 3 4 5
[3, 4, 5]
[3, 4, 5]

[Program finished]

Upvotes: 1

Views: 439

Answers (1)

koo5
koo5

Reputation: 485

you are not printing rslt :) If you make it print rslt, you will get this output:

Enter a number list 1 2 3 4
[1, 2, 3, 4]
[2, 1.0, 6, 2.0]

Upvotes: 1

Related Questions