Carlton
Carlton

Reputation: 973

Return list of items in list greater than some value

I have the following list

j=[4,5,6,7,1,3,7,5]

What's the simplest way to return [5,5,6,7,7] being the elements in j greater or equal to 5?

Upvotes: 97

Views: 291589

Answers (7)

Michael Mrozek
Michael Mrozek

Reputation: 175705

You can use a list comprehension to filter it:

j2 = [i for i in j if i >= 5]

If you actually want it sorted like your example was, you can use sorted:

j2 = sorted(i for i in j if i >= 5)

Or call sort on the final list:

j2 = [i for i in j if i >= 5]
j2.sort()

Upvotes: 136

In case you are considering using the NumPy module, it makes this task very simple, as requested:

import numpy as np

j = np.array([4, 5, 6, 7, 1, 3, 7, 5])

j2 = np.sort(j[j >= 5])

The code inside of the brackets, j >= 5, produces a list of True or False values, which then serve as indices to select the desired values in j. Finally, we sort with the sort function built into NumPy.

Tested result (a NumPy array):

array([5, 5, 6, 7, 7])

Upvotes: 3

Harsha VK
Harsha VK

Reputation: 11

There is another way,

j3 = j2 > 4
print(j2[j3])

It was tested in Python 3.

Upvotes: 0

U13-Forward
U13-Forward

Reputation: 71610

Use filter (short version without doing a function with lambda, using __le__):

j2 = filter((5).__le__, j)

Example (Python 3):

>>> j=[4,5,6,7,1,3,7,5]
>>> j2 = filter((5).__le__, j)
>>> j2
<filter object at 0x000000955D16DC18>
>>> list(j2)
[5, 6, 7, 7, 5]
>>>

Example (Python 2):

>>> j=[4,5,6,7,1,3,7,5]
>>> j2 = filter((5).__le__, j)
>>> j2
[5, 6, 7, 7, 5]
>>>

Use __le__. I recommend this. It's very easy. __le__ is your friend.

If want to sort it to the desired output (both versions):

>>> j=[4,5,6,7,1,3,7,5]
>>> j2 = filter((5).__le__, j)
>>> sorted(j2)
[5, 5, 6, 7, 7]
>>>

Use sorted

Timings:

>>> from timeit import timeit
>>> timeit(lambda: [i for i in j if i >= 5]) # Michael Mrozek
1.4558496298222325
>>> timeit(lambda: filter(lambda x: x >= 5, j)) # Justin Ardini
0.693048732089828
>>> timeit(lambda: filter((5).__le__, j)) # Mine
0.714461565831428
>>>

So Justin wins!!

With number=1:

>>> from timeit import timeit
>>> timeit(lambda: [i for i in j if i >= 5],number=1) # Michael Mrozek
1.642193421957927e-05
>>> timeit(lambda: filter(lambda x: x >= 5, j),number=1) # Justin Ardini
3.421236300482633e-06
>>> timeit(lambda: filter((5).__le__, j),number=1) # Mine
1.8474676011237534e-05
>>>

So Michael wins!!

>>> from timeit import timeit
>>> timeit(lambda: [i for i in j if i >= 5],number=10) # Michael Mrozek
4.721306089550126e-05
>>> timeit(lambda: filter(lambda x: x >= 5, j),number=10) # Justin Ardini
1.0947956184281793e-05
>>> timeit(lambda: filter((5).__le__, j),number=10) # Mine
1.5053439710754901e-05
>>>

So Justin wins again!!

Upvotes: 5

Lennart Regebro
Lennart Regebro

Reputation: 172377

Since your desired output is sorted, you also need to sort it:

>>> j=[4, 5, 6, 7, 1, 3, 7, 5]
>>> sorted(x for x in j if x >= 5)
[5, 5, 6, 7, 7]

Upvotes: 1

Justin Ardini
Justin Ardini

Reputation: 9866

A list comprehension is a simple approach:

j2 = [x for x in j if x >= 5]

Alternately, you can use filter for the exact same result:

j2 = filter(lambda x: x >= 5, j)

Note that the original list j is unmodified.

Upvotes: 19

sepp2k
sepp2k

Reputation: 370435

You can use a list comprehension:

[x for x in j if x >= 5]

Upvotes: 13

Related Questions