Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

python filter array

myList = [1, 2, 3, 4, 5]
filtered = list(filter(lambda x: x%2==0, myList))
#                      ^ ^ ^ ^ ^ ^ ^ ^ ^
#                 function that returns boolean

# OR

filtered = [x for x in myList if x%2==0]
#                                ^ ^ ^
#                              condition
Comment

filter elements array python

friends = [
    {'name': 'Sam', 'gender': 'male', 'sport': 'Basketball'},
    {'name': 'Emily', 'gender': 'female', 'sport': 'Volleyball'}
]
# filter by column names
print([{key: friend[key] for key in friend.keys() if key in ["name", "sport"]} for friend in friends])
# filter by rows
print([a for a in friends if a["name"] in ["Sam"]])
Comment

PREVIOUS NEXT
Code Example
Python :: how to read the first line in a file python 
Python :: install easygui 
Python :: bee movie script 
Python :: cv2 draw box 
Python :: normalize values between 0 and 1 python 
Python :: python password generator 
Python :: python half of string 
Python :: python messagebox 
Python :: python ping ip address 
Python :: how to lowercase list in python 
Python :: how to save matplotlib figure to png 
Python :: how to get frequency of each elements in a python list 
Python :: save machine learning model python 
Python :: pandas select by column value 
Python :: python roman to integer 
Python :: selenium-screenshot python 
Python :: A value is trying to be set on a copy of a slice from a DataFrame. 
Python :: load custom font pygame 
Python :: how to plot roc curve in python 
Python :: no python 3.10 installation was detected 
Python :: f string round 
Python :: how to check opencv version using python 
Python :: python opencv write text on image 
Python :: py get days until date 
Python :: python read url 
Python :: save list python 
Python :: make dataframe from list of tuples 
Python :: run JupyterLab 
Python :: how to play music on pygame 
Python :: python pandas apply to one column 
ADD CONTENT
Topic
Content
Source link
Name
5+5 =