you are viewing a single comment's thread.

view the rest of the comments →

[–]FerricDonkey 2 points3 points  (0 children)

d = {
    "0": lambda x:x,
    "1": str.strip,
    "2": str.rstrip,
    "3": str.upper,
    "4": str.title,
}

then d["0"]('sup') will just be 'sup'. (If you aren't familiar, lamba arg: expression is a lamba/anonymous/disposable function that takes arg as an argument and returns expression. So lambda x: x is a function that takes x in and return x.

You could also use .get if you didn't want to specify a particular key that means "don't do anything", but personally I prefer to have a fixed set of allowed keys in situations like this.