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 :: como poner python 3 en la terminal mac 
Python :: manim replacement transform 
Python :: why do we need to preprocess data 
Python :: Django-rest-framework-simplejwt.readthedocs.io 
Python :: if string contains loop pandas 
Python :: python basic programs area caluclation 
Python :: python getting line length using list comprehension 
Python :: python Access both key and value without using items() 
Python :: Bilgisayardaki mp3 uzantili dosyalari bulma 
Python :: list of ones python 
Python :: python sort_values 
Python :: how a 16 mp camera looks like 
Python :: how to change pi hostname in python file 
Python :: how to save all countries from a list in a database python 
Python :: forward fill pandas ffill 
Python :: how to make ui dialog pop in front pyqt 
Python :: add python 3.9 to usr/bin 
Python :: how to draw play area for a game in python turtle 
Python :: gcp jupyter use python variables in magic bigquery 
Python :: pandas get data from upper row 
Python :: xml to sqlite 
Python :: python einops rearrange 
Python :: python empty list boolean 
Python :: plotly two y axis bar chart 
Python :: Get index for value_counts() 
Python :: for loop python terminal 
Python :: genrate requirments.txt pytohn 
Python :: th most effective search methods in python with example 
Python :: plotting a dendrogram from the distance matrix 
Python :: How to use a function output as an input of another function in Python 
ADD CONTENT
Topic
Content
Source link
Name
1+4 =