Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR PYTHON

list comperhension condition in python

numbers = [1, 2, 3, 4, 5, 6]
a = [num * 2 if num % 2 == 0 else num/2 for num in numbers] # [0.5, 4, 1.5, 8, 2.5, 12]
bEven = [num for num in numbers if num % 2 == 0] # 2, 4, 6
bOdd = [num for num in numbers if num % 2 != 0] # 1, 3, 5
Source by stackoverflow.com #
 
PREVIOUS NEXT
Tagged: #list #comperhension #condition #python
ADD COMMENT
Topic
Name
8+6 =