How to return a subset of a list that matches a condition
Use list comprehension,
divisibleBySeven = [num for num in inputList if num != 0 and num % 7 == 0]
or you can use the meetsCondition
also,
divisibleBySeven = [num for num in inputList if meetsCondition(num)]
you can actually write the same condition with Python's truthy semantics, like this
divisibleBySeven = [num for num in inputList if num and num % 7]
alternatively, you can use filter
function with your meetsCondition
, like this
divisibleBySeven = filter(meetsCondition, inputList)
%timeit
listOfNumbers = range(1000000)
%timeit [num for num in listOfNumbers if meetsCondition(num)]
[out]:
243 ms ± 4.51 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)
%timeit list(filter(meetsCondition, listOfNumbers))
[out]:
211 ms ± 4.19 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)
How to pick up all the elements from a python list that meet certain criteria?
Using np.where
:
>>> import numpy as np
>>> l = [0.2, 0.4, 0.5]
# Case list into numpy array type.
>>> l_arr = np.array(l)
# Returns the indices that meets the condition.
>>> np.where(l_arr > 0.3)
(array([1, 2]),)
# Returns the values that meets the condition.
>>> l_arr[np.where(l_arr > 0.3)]
array([ 0.4, 0.5])
Python for element in list matching condition
Not quite the syntax you are after but you can also use filter
using a lambda:
for x in filter(lambda y: y > 2, foo):
print(x)
Or using a function for readbility sake:
def greaterthantwo(y):
return y > 2
for x in filter(greaterthantwo, foo):
print(x)
filter
also has the advantage of creating a generator expression so it doesn't evaluate all the values if you exit the loop early (as opposed to using a new list)
Extract values from a list based on a condition
Or, the other way:
>>> a = [2,4,5,9,1,6,4]
>>> b = 6
>>> c = filter(lambda x: x < b, a)
>>> c
[2, 4, 5, 1, 4]
You almost had it as Ignacio pointed out:
>>> c = [x for x in a if x < b]
>>> c
[2, 4, 5, 1, 4]
The list comprehension is a longer way of writing this loop:
>>> c = []
>>> for x in a:
... if x < b:
... c.append(x)
...
>>> c
[2, 4, 5, 1, 4]
How can I return items that match a condition?
You could use filter
method by passing a lambda function.
const activeStep = 1;
{filteredData.filter((el) => activeStep == 1 ? el.isSelected : true).map((el) => (
<MarketItem el={el} />
))}
Subset elements in a list based on a logical condition
[
is expecting a vector, so use unlist
on cond
:
l[unlist(cond)]
$b
[1] 4 5 6 5
$c
[1] 3 4 5 6
How to retrieve partial matches from a list of strings
startswith
andin
, return a Boolean.- The
in
operator is a test of membership. - This can be performed with a
list-comprehension
orfilter
. - Using a
list-comprehension
, within
, is the fastest implementation tested. - If case is not an issue, consider mapping all the words to lowercase.
l = list(map(str.lower, l))
.
- Tested with python 3.11.0
filter
:
- Using
filter
creates afilter
object, solist()
is used to show all the matching values in alist
.
l = ['ones', 'twos', 'threes']
wanted = 'three'
# using startswith
result = list(filter(lambda x: x.startswith(wanted), l))
# using in
result = list(filter(lambda x: wanted in x, l))
print(result)
[out]:
['threes']
list-comprehension
l = ['ones', 'twos', 'threes']
wanted = 'three'
# using startswith
result = [v for v in l if v.startswith(wanted)]
# using in
result = [v for v in l if wanted in v]
print(result)
[out]:
['threes']
Which implementation is faster?
- Tested in Jupyter Lab using the
words
corpus fromnltk v3.7
, which has 236736 words - Words with
'three'
['three', 'threefold', 'threefolded', 'threefoldedness', 'threefoldly', 'threefoldness', 'threeling', 'threeness', 'threepence', 'threepenny', 'threepennyworth', 'threescore', 'threesome']
from nltk.corpus import words
%timeit list(filter(lambda x: x.startswith(wanted), words.words()))
%timeit list(filter(lambda x: wanted in x, words.words()))
%timeit [v for v in words.words() if v.startswith(wanted)]
%timeit [v for v in words.words() if wanted in v]
%timeit
results
62.8 ms ± 816 µs per loop (mean ± std. dev. of 7 runs, 10 loops each)
53.8 ms ± 982 µs per loop (mean ± std. dev. of 7 runs, 10 loops each)
56.9 ms ± 1.33 ms per loop (mean ± std. dev. of 7 runs, 10 loops each)
47.5 ms ± 1.04 ms per loop (mean ± std. dev. of 7 runs, 10 loops each)
Return table with count of elements that match a condition
Subset the data for "PASS" value and then use table
:
temp <- subset(df, Outcome == 'PASS')
table(temp$Participant, temp$Trial)
# T01 T02 T03
# P01 2 0 1
# P02 1 2 1
# P03 0 2 1
How can I partition (split up, divide) a list based on a condition?
good = [x for x in mylist if x in goodvals]
bad = [x for x in mylist if x not in goodvals]How can I do this more elegantly?
That code is already perfectly elegant.
There might be slight performance improvements using set
s, but the difference is trivial. set
based approaches will also discard duplicates and will not preserve the order of elements. I find the list comprehension far easier to read, too.
In fact, we could even more simply just use a for
loop:
good, bad = [], []
for x in mylist:
if x in goodvals:
good.append(f)
else:
bad.append(f)
This approach makes it easier to add additional logic. For example, the code is easily modified to discard None
values:
good, bad = [], []
for x in mylist:
if x is None:
continue
if x in goodvals:
good.append(f)
else:
bad.append(f)
Related Topics
Define a Method Outside of Class Definition
How to Assign a Variable in an If Condition, and Then Return It
Import Script from a Parent Directory
Open Cv Error: (-215) Scn == 3 || Scn == 4 in Function Cvtcolor
Better Way to Shuffle Two Numpy Arrays in Unison
How to Draw a Line with Matplotlib
Using Monotonically_Increasing_Id() for Assigning Row Number to Pyspark Dataframe
Writing to a File in a for Loop Only Writes the Last Value
How to Find Out My Pythonpath Using Python
Pyplot Move Alternative Y Axis to Background
Generating Discrete Random Variables with Specified Weights Using Scipy or Numpy
Pygame Tic Tak Toe Logic? How Would I Do It
How to Efficiently Process a Numpy Array in Blocks Similar to Matlab's Blkproc (Blockproc) Function