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
The simplest way to check if a dictionary has exactly 2 key-value pairs, where both keys and values are strings (
str), is to use the following Python code:def is_dict_with_two_str_pairs(d): return ( isinstance(d, dict) and len(d) == 2 and all(isinstance(k, str) and isinstance(v, str) for k, v in d.items()) )Explanation:
isinstance(d, dict): Checks if the input is a dictionary.len(d) == 2: Ensures the dictionary has exactly 2 key-value pairs.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.