How to Return a Subset of a List That Matches a Condition

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 and in, return a Boolean.
  • The in operator is a test of membership.
  • This can be performed with a list-comprehension or filter.
  • Using a list-comprehension, with in, 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 a filter object, so list() is used to show all the matching values in a list.
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 from nltk 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 sets, 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



Leave a reply



Submit