def listi(list_, elem, default=None):
    """
    Return the elem component in a list of lists, or list of tuples, or list of dicts.
    If default is non-None then if the key is missing return that.

    Examples:
        l = [("A", "B"), ("C", "D")]
        listi(l, 1) == ["B", "D"]

        l = [{"A":1, "B":2}, {"A":3, "B", 4}, {"Q": 5}]
        listi(l, "B", default=0) == [2, 4, 0]
    """
    ret = []
    for i in list_:
        if isinstance(i, dict):
            if elem in i:
                ret += [i[elem]]
            elif default is not None:
                ret += [default]
            else:
                raise KeyError("Missing elem in list of dicts")
        else:
            if 0 <= elem < len(i):
                ret += [i[elem]]
            elif default is not None:
                ret += [default]
            else:
                raise KeyError("Missing elem in list of lists")

    return ret