Frage

Say if I have the following:

mydict = {
   a: 'A'
}

How do I check if key a exists in the dictionary? Pseudocode below:

%if 'a' in mydict.keys()
  ${mydict['a']}
%endif
War es hilfreich?

Lösung

You can just use in:

from mako.template import Template
t = Template("""
% if key in d:
    key is in dictionary
% else:
    key is not in dictionary
% endif
""")


print t.render(key='a', d={'a': 'A'})  # prints "key is in dictionary"
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top