What's the idiomatic python equivalent of get() for lists?
问题 Calling get(key) on a dictionary will return None by default if the key isn't present in a dictionary. What is the idiomatic equivalent for a list, such that if a list is of at least size of the passed in index the element is returned, otherwise None is returned? To rephrase, what's a more idiomatic/compact version of this function: def get(l, i): if i < len(l): return l[i] else: return None 回答1: Your implementation is Look Before You Leap-style. It's pythonic to execute the code and catch