Reputation: 1991
I have a dictionary as below :
'give': (('VBP', 6), ('VB', 15)),
'discounts': ('NNS', 1),
'maintaining': ('VBG', 4),
'increasing': ('VBG', 18),
'spending': (('NN', 24), ('VBG', 2)),
'become': ((('VBN', 7), ('VB', 15)), ('VBP', 1)),
'permanent': ('JJ', 2),
'fixtures': ('NNS', 1),
'news': ('NN', 24),
'weeklies': ('NNS', 2),
'underscore': ('VBP', 1),
'fierce': ('JJ', 2),
'competition': ('NN', 10)
I am writing a list comprehension as below :
result = [x for x in mydict.items() if type(x[1][0]) == 'str']
But this results in an empty list, whereas if I see there are many elements in dictionary, for which this condition satisfies.
Upvotes: 1
Views: 94
Reputation: 18208
Either you can change 'str'
to str
i.e.
result = [x for x in mydict.items() if type(x[1][0]) == str]
or may be you can try with isinstance
method to check if it is the instance of string
(details):
result = [x for x,value in mydict.items() if isinstance(x[1][0],str)]
print(result)
Result:
['increasing', 'maintaining', 'fierce', 'permanent', 'fixtures', 'underscore', 'news', 'weeklies', 'discounts', 'become', 'give', 'competition', 'spending']
Upvotes: 3