Question:

Is it pythonic to pass a list of dictionary keys to iterate, BUT if an empty list is passed, iterate the entire dictionary?

If not suggestions please 😅

My only other approach is two methods, but as there are many features to process this way, it seems overly messy to me.

#python`

@kingfisher I'm no Python guru, so take this with a grain of salt, but it feels wrong to me to treat an empty list of keys as implicitly a list of every key.

Would it ever (EVER!) make sense to pass an empty list and expect no iteration to occur? I.e., is it possible, even if very improbable, that a caller would generate a legitimate empty list of keys and be surprised that the call iterated over the entire dictionary? If so, don't make an empty list mean "every key".

It feels like an optional parameter or a special value like "Undefined" or "None" would be better than an empty list. That way it is explicit that you are NOT passing a list.

@kauer Agreed, my suggested empty list does not sound right.

"None" would be better, but it still feels feels a bit like the empty list in that it is not obvious what None means.

My first idea which I forgot to mention, was an additional parameter to flag "iterate all", but that too is confusing if a list is passed....and consequently ignored.

@kingfisher If they pass inconsistent parameters the method should return an error or throw an exception. Can you pass different parameter lists in different calls? Passed a list, it iterates over those keys in the dictionary. Passed nothing, it iterates over the whole dictionary. Call the methiod "iterate_dict()" and the parameter "include_only" to make it clearer what's going on. Also makes it obvious that if they send an empty include_only list, they'll get nothing 🙂
@kingfisher a "filter" keyword argument which takes a function is my guess on the pythonic way to handle this but I don't know if that's what I would prefer until I saw more code.

@kingfisher one function returns which keys to operate over. If it's all of them, it returns all of them.

A second function iterates over the list of keys it is given.

@tartley As usual I think I have overcomplicated situation.

Based on everyones feedback, now my current thinking is all pass a list and if list empty - give warning.....

To handle the ALL situation just pass the list as the_dict.keys(), which, sigh, I am already doing in many other places.

@kingfisher fair enough! Don't fret, overcomplicating things is every programmer's default mode of operation.🤗

@tartley Then despite the evidence I must be a great programmer!!

Thanks everyone

@kingfisher

In terms of actual function signature, it doesn't seem very Pythonic to me. This would be clearer, I think:

def process_dict(d: dict[str, str], keys: list[str] | None = None) -> whatever:

Having `keys` be a second, optional argument that defaults to None (so it can't be mistaken for a list) more strongly implies the purpose of that argument, I think. If you supply keys, process those, otherwise process the whole thing.

@cazabon Indeed that is a good function signature.

I have been lazy in both code and my question here and need to relearn+use those 'modern' function signatures and in syudy particular the optional value, ie ' | None = None' etc