I categorized keywords as following nest list,
Keywords_33=[('File_2', ['with', 'as']),
('Module_2', ['from', 'import']),
('Constant_3', {'bool': ['False', 'True'],
'none': ['None']}),
('Operator_4', {'boolean_operation': {'or', 'and', 'not'},
'comparison': {'is'}}),
('Container_operation_2', ['in', 'del']),
('Klass_1', ['class']),
('Function_7',['lambda', 'def', 'pass',
'global', 'nonlocal',
'return', 'yield']),
('Repetition_4', ['while', 'for', 'continue', 'break']),
('Condition_3', ['if', 'elif', 'else']),
('Debug_2', ['assert', 'raise']),
('Exception_3', ['try', 'except', 'finally'])]
I intent to confirm every keyword in place by category without any left.
The most convenient I consider is to convert Keywords_33
to string firstly.
from keyword import kwlist
In [55]: print(kwlist)
['False', 'None', 'True', 'and', 'as', 'assert', 'break', 'class', 'continue', 'def', 'del', 'elif', 'else', 'except', 'finally', 'for', 'from', 'global', 'if', 'import', 'in', 'is', 'lambda', 'nonlocal', 'not', 'or', 'pass', 'raise', 'return', 'try', 'while', 'with', 'yield']
In [54]: from keyword import kwlist
...: s = str(Keywords_33)
...: [keyword for keyword in kwlist if keyword not in s]
...:
Out[54]: []
# It indicate no keyword left
How to accomplish such a task elegantly?