Use a list comprehension:

[y for x,y in A if x>2]

Demo:

>>> A=[(1,'A'),(2,'H'),(3,'K'),(4,'J')]
>>> [y for x,y in A if x>2]
['K', 'J']
>>> 
Answer from U13-Forward on Stack Overflow
๐ŸŒ
KDnuggets
kdnuggets.com โ€บ 2022 โ€บ 11 โ€บ 5-ways-filtering-python-lists.html
5 Ways of Filtering Python Lists - KDnuggets
November 14, 2022 - It is easy to write, and you can even add multiple if-else conditions without an issue. Learn list comprehension with code examples by reading When to Use a List Comprehension in Python. scores = [200, 105, 18, 80, 150, 140] filtered_scores = [s for s in scores if s >= 150] print(filtered_scores)
Discussions

Filtering list of objects on multiple conditions
'm looking for the most straightforward and intuitive way to filter these values. not sure there is an "in-between" where you avoid all the work but also get the functionality.. More on reddit.com
๐ŸŒ r/learnpython
4
1
July 27, 2023
Filter instance of a class with a list of conditions
๐ŸŒ r/learnpython
Querying Flask SQL Alchemy by multiple parameters
Pretty sure it's: Search.query.filter_by(user_input=query, location = location).first() More on reddit.com
๐ŸŒ r/flask
7
6
August 26, 2018
Multiple conditions in python filter function
No, why would you expect your output to include 2 when your condition explicitly says x!=2? The other condition excludes any even number, since an odd number modulus 2 returns 1, and 1 converts to True. 0 converts to False. So, you're explicitly filtering out any number evenly divisible by 2. More on reddit.com
๐ŸŒ r/learnprogramming
4
1
January 7, 2019
๐ŸŒ
Real Python
realpython.com โ€บ lessons โ€บ filtering-elements-list-comprehensions
Filtering Elements in List Comprehensions (Video) โ€“ Real Python
Conditional statements can be added to Python list comprehensions in order to filter out data. In this lesson, you learned how to use filtering to produce a list of even squares. The returned data is the same as before except for the fact that only even squares are returned.
Published ย  March 1, 2019
๐ŸŒ
DEV Community
dev.to โ€บ bybydev โ€บ top-3-methods-to-filter-a-list-in-python-19fe
Top 3 methods to filter a list in Python - DEV Community
May 3, 2024 - Python offers versatile list filtering through list comprehensions, filter() function with lambda expressions, or comprehensible conditional statements for concise data manipulation.
๐ŸŒ
LabEx
labex.io โ€บ tutorials โ€บ python-how-to-filter-list-with-conditions-419442
Python - How to filter list with conditions
By understanding these basics, you'll be well-equipped to handle list filtering in your Python projects with LabEx's powerful learning resources. Python allows complex filtering using logical operators: ## Multiple condition filtering data = [10, 15, 20, 25, 30, 35, 40] filtered_data = [x for x in data if x > 20 and x < 40] print(filtered_data) ## Output: [25, 30, 35]
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ python โ€บ filter-in-python
filter() in python - GeeksforGeeks
def even(n): return n % 2 == 0 a = [1, 2, 3, 4, 5, 6] b = filter(even, a) print(list(b)) # Convert filter object to a list ... Only even numbers are included in output. Example 2: Below example uses a lambda function with filter() to select ...
Published ย  1 week ago
๐ŸŒ
Mimo
mimo.org โ€บ glossary โ€บ python โ€บ filter
Python filter(): Syntax, Usage, and Examples
You should reach for filter() when you want to streamline conditional selection. It's especially useful for: Keeping only valid data entries from a list or array ... Instead of managing temporary lists and for loops manually, you express your filtering logic in one line. This approach is similar to filtering in other languages like Java or SQL, but with Python's more concise syntax.
Find elsewhere
๐ŸŒ
Reddit
reddit.com โ€บ r/learnpython โ€บ filtering list of objects on multiple conditions
r/learnpython on Reddit: Filtering list of objects on multiple conditions
July 27, 2023 -

Hi all, interested in your approaches and suggestions to this problem.

Let's say I have created a custom Player class that contains many attributes relating to a particular sportsperson's characteristics:

@dataclass
class Player:
    id: int
    first_name: str
    last_name: str
    age: int
    height: int
    weight: int
    team: int
    games_played: int
    ...

If I create a small (~500) length list of these objects and then want to filter on multiple conditions, what's the best way for me to do that?

Of course, in a one off situation I could write something like:

filtered = [player for player in player_list if 20 <= player.age < 25 and player.height > 180 and player.team == "Rovers"]

This gives me the flexibility to filter exactly how I'd like but it's not the most glamorous solution in my opinion, especially if I want to try out lots of different filters.

I had a thought that this might be easier to by creating something like a PlayerFilter class which can be instantiated and then have filters added to a dict attribute or similar, and then can be applied to a list of players and return the filtered list. This seems more Pythonic to me but also seems slightly overkill and I'm looking for the most straightforward and intuitive way to filter these values. I'm imagining if a random python user was to use my package with this filter implementation, I'm not sure if that would make sense.

Is the complexity a side effect of using a list of objects to store my data as opposed to something like a dataframe? I've considered this too but I want to maintain the ability to call the methods that I've created for my custom player class on any filtered output.

Let me know what you think!

๐ŸŒ
Finxter
blog.finxter.com โ€บ home โ€บ learn python blog โ€บ how to filter a list in python?
How to Filter a List in Python? - Be on the Right Side of Change
July 14, 2022 - How can you filter a list in Python using an arbitrary condition? The most Pythonic and most performant way is to use list comprehension [x for x in list if condition] to filter all elements from a list.
๐ŸŒ
Spark By {Examples}
sparkbyexamples.com โ€บ home โ€บ python โ€บ filter elements from python list
Filter Elements from Python List - Spark By {Examples}
May 31, 2024 - The filter() is a built-in function in Python that is used to filter the elements from the iterable object like list, set e.t.c. This function will directly filter the elements in a list by taking the condition as a parameter.
๐ŸŒ
Programiz
programiz.com โ€บ python-programming โ€บ methods โ€บ built-in โ€บ filter
Python filter()
Become a certified Python programmer. Try Programiz PRO! ... The filter() function selects elements from an iterable based on the result of a function. # returns True if the argument passed is even def check_even(number): if number % 2 == 0: return True return False numbers = [1, 2, 3, 4, 5, ...
๐ŸŒ
ZetCode
zetcode.com โ€บ python โ€บ filter-list
Python filter list - filtering lists in Python
The filter is a built-in function which returns iterator from those elements of iterable that satisfy the given predicate. The function predates list comprehensions; it is generally recommended to use list comprehensions. ... #!/usr/bin/python words = ['sky', 'cloud', 'wood', 'forest', 'tie', ...
๐ŸŒ
FavTutor
favtutor.com โ€บ blogs โ€บ filter-list-python
Python filter() Function: 5 Best Methods to Filter a List
November 11, 2023 - It is easy to write clean code, and you can even add multiple if-else conditions without any difficulty. numbers = [70, 60, 80, 90, 50,82,90,91,84,82,94,99,78,65,61,45,89,87,49,76,81,94] #function to check scores above 80 filtered_list = [ i for i in numbers if i>= 80] print(filtered_list)
๐ŸŒ
Codingem
codingem.com โ€บ home โ€บ how to filter lists in python
How to Filter a List in Python [in 3 SIMPLE Ways] - codingem.com
November 2, 2022 - To filter lists in Python, use filter(). For example, filter(lambda age: age > 20, ages) filters an ages list so that ages 20+ only are left.
๐ŸŒ
W3Schools
w3schools.com โ€บ python โ€บ numpy โ€บ numpy_array_filter.asp
NumPy Filter Array
In the example above we hard-coded the True and False values, but the common use is to create a filter array based on conditions. Create a filter array that will return only values higher than 42: import numpy as np arr = np.array([41, 42, 43, 44]) # Create an empty list filter_arr = [] # go through each element in arr for element in arr: # if the element is higher than 42, set the value to True, otherwise False: if element > 42: filter_arr.append(True) else: filter_arr.append(False) newarr = arr[filter_arr] print(filter_arr) print(newarr) Try it Yourself ยป
๐ŸŒ
Note.nkmk.me
note.nkmk.me โ€บ home โ€บ python
Filter (Extract/Remove) Items of a List with in Python: filter() | note.nkmk.me
May 15, 2023 - In Python, you can use filter() to filter (extract/remove) items of a list, tuple, or other iterable that satisfy the conditions. Built-in Functions - filter() โ€” Python 3.11.3 documentation Basic usa ...
๐ŸŒ
Towards Data Science
towardsdatascience.com โ€บ home โ€บ latest โ€บ filtering lists in python
Filtering Lists in Python | Towards Data Science
January 27, 2025 - Specifically, we will walk through how to use list comprehension, generator expressions and the built-in โ€˜filter()โ€™ method to filter lists in python.
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ python-filter-a-list-based-on-the-given-list-of-strings
Filter a list based on the Given List of Strings - Python - GeeksforGeeks
February 13, 2025 - Explanation: list comprehension ... The remaining paths are stored in res . This method applies a filtering condition using the filter() combined with a lambda expression....
๐ŸŒ
Optymize
optymize.io โ€บ home โ€บ 8 best methods to use python filter list
8 Best Methods To Use Python Filter List | Optymize
March 24, 2023 - You have to use the Python filter list function to filter the dictionaries by the value of the dictionary key(attribute). How to do this? Letโ€™s say you have three user dictionaries- username, age, and play_time keys.