Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

list comprehension python with condition

[f(x) if condition else g(x) for x in sequence]
Comment

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
Comment

PREVIOUS NEXT
Code Example
Python :: import database in python using sqlalchemy 
Python :: view all columns in pandas dataframe 
Python :: swagger library for django 
Python :: numpy.ndarray to lsit 
Python :: django check user admin 
Python :: tkinter simple notification 
Python :: python find largest variable 
Python :: python iterate list 
Python :: python if any element in string 
Python :: git help 
Python :: connect to spark cluster 
Python :: pandas drop if present 
Python :: catch error data with except python 
Python :: make a list in python 3 
Python :: python find string 
Python :: directory path with python argparse 
Python :: headless chrome python 
Python :: python pandas convert series to percent 
Python :: matplotlib show plot 
Python :: current date and time django template 
Python :: subtract from dataframe column 
Python :: python pipe 
Python :: python isinstance list 
Python :: python b before string 
Python :: turn off warning when import python 
Python :: python dict setdefault 
Python :: python code with sigma 
Python :: python create path 
Python :: read value from entry tkinter 
Python :: MAKE A SPHERE IN PYTHON 
ADD CONTENT
Topic
Content
Source link
Name
2+3 =