Re: list of dictionaries search using kwargs

2020-12-07 Thread Marco Sulla
On Mon, 7 Dec 2020 at 23:35, Larry Martell wrote: > > On Mon, Dec 7, 2020 at 5:29 PM Marco Sulla > wrote: > > > > You can return dictionaries that returns True if > > > > (a.items() & kwargs.items()) == kwargs.items() > > > > when `a` is one of your dicts. > > But what is passed in kwargs will n

Re: list of dictionaries search using kwargs

2020-12-07 Thread MRAB
On 2020-12-07 22:06, Larry Martell wrote: I have a class that has an object that contains a list of dicts. I want to have a class method that takes a variable number of key/value pairs and searches the list and returns the item that matches the arguments. If I know the key value pairs I can do s

Re: list of dictionaries search using kwargs

2020-12-07 Thread Larry Martell
On Mon, Dec 7, 2020 at 5:42 PM Matt Wheeler wrote: > > for item in self.data: > if all(item[k] == v for k,v in kwargs.items()): > return item > > Or > > return [item for item in self.data if all(item[k] == v for k,v in > kwargs.items())] > > to return all matches > > Beware though tha

Re: list of dictionaries search using kwargs

2020-12-07 Thread Matt Wheeler
for item in self.data:     if all(item[k] == v for k,v in kwargs.items()):         return item Or return [item for item in self.data if all(item[k] == v for k,v in kwargs.items())] to return all matches Beware though that either of these will be slow if your list of dicts is large. If the list

Re: list of dictionaries search using kwargs

2020-12-07 Thread Larry Martell
On Mon, Dec 7, 2020 at 5:29 PM Marco Sulla wrote: > > You can return dictionaries that returns True if > > (a.items() & kwargs.items()) == kwargs.items() > > when `a` is one of your dicts. But what is passed in kwargs will not necessarily have values for all of the keys and I only want to check f

Re: list of dictionaries search using kwargs

2020-12-07 Thread Marco Sulla
You can return dictionaries that returns True if (a.items() & kwargs.items()) == kwargs.items() when `a` is one of your dicts. -- https://mail.python.org/mailman/listinfo/python-list

list of dictionaries search using kwargs

2020-12-07 Thread Larry Martell
I have a class that has an object that contains a list of dicts. I want to have a class method that takes a variable number of key/value pairs and searches the list and returns the item that matches the arguments. If I know the key value pairs I can do something like this: instance = next(item fo