o3-2025-04-16· OpenAI
LMArena Human Preference 140K
what would be the simplest way in python to check if a given dict is composed by exactly…
- User
what would be the simplest way in python to check if a given dict is composed by exactly 2 key value pairs that are all str ?
- Assistant
Here’s a one-liner that does the job:
def is_two_str_pairs(d: dict) -> bool: return ( isinstance(d, dict) # it is a dict and len(d) == 2 # exactly two pairs and all( isinstance(k, str) and isinstance(v, str) for k, v in d.items() # every key and value is str ) )Usage:
>>> is_two_str_pairs({'a': 'x', 'b': 'y'}) True >>> is_two_str_pairs({'a': 'x'}) # only 1 pair False >>> is_two_str_pairs({'a': 1, 'b': 'y'}) # non-string value False >>> is_two_str_pairs({1: 'x', 2: 'y'}) # non-string keys False
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.