Functional way of treating lists in Python