This block would be much cleaner as a small dict comprehension. Also is there a reason why you're casting var to a string? If there is it deserves a comment.