on-internet
← Community

Pythonic way to obtain dictionary keys with desired property

Stack Overflow ·

In python 3.12 I have a dictionary of which the keys are 2-tuples of integers and of which the values are integers (as in the function f:N × N → N for which f(n, m) = n + m). What I need is those keys of the dictionary where the first element has a specific value. It's simple enough to write a function like def matching_keys(d: dict[tuple[int, int], int], n: int) -> list[tuple[int, int]]: """ find all the keys in a dictionary *d* whose first element is the integer *n* :param d: the dictionary in question :type d: dict[tuple[int, int], int] :param n: the first-element value *n* in question :typ

Comments

Sign in or sign up to comment and upvote.

No comments yet.