Is there a "clean" way to take the type of dictionary keys in python3?
For example, I want to decide if one of these dictionaries has keys like str:
d1 = { 1:'one', 2:'two', 5:'five' }
d2 = { '1':'one', '2':'two', '5':'five' }
There are several ways to achieve this, for example, using some of them:
isinstance(list(d2.keys())[0], type('str'))
But this is quite annoying because it is d2.keys()not indexed, so you need to convert it to a list in order to extract the key value.
So python3 is something like get_key_type(d2)? If not, is there a better (cleaner) way to find out if the dictionary key has a type str?
source
share