Using python lambda funtions and removing the redundant lines of code