Repository: yt-dlp/yt-dlp
File: yt_dlp/utils.py (Python)

Code before:
                        result_objs.extend(str(obj))

                elif callable(key):
                    has_branched = True
                    if is_sequence(obj):
                        iter_obj = enumerate(obj)
                    elif isinstance(obj, collections.abc.Mapping):
                        iter_obj = obj.items()
                    elif traverse_string:
                        iter_obj = enumerate(str(obj))
                    else:
                        iter_obj = ()
                    result_objs.extend(v for k, v in iter_obj if try_call(key, args=(k, v)))

                elif isinstance(key, dict):
                    obj = [(k, _traverse_obj(obj, v)) for k, v in key.items()]
                    result_objs.append({k: v if v is not None else default for k, v in obj
                                        if v is not None or (v is None and default is not None)})

                elif isinstance(obj, dict):
                    if casesense:
                        result = obj.get(key)
                    else:
                        result = next((v for k, v in obj.items() if _casefold(k) == key), None)
                    if result is not None:
                        result_objs.append(result)

                else:
                    if is_user_input:
                        assert isinstance(key, str)
                        if ':' not in key:
                            key = int_or_none(key)
                        else:
                            key = slice(*map(int_or_none, key.split(':')))

                    if not isinstance(key, (int, slice)):
                        continue

                    if not is_sequence(obj):
                        if not traverse_string:
                            continue

                        obj = str(obj)

                    try:
                        result = obj[key]
                        if result is not None:
                            result_objs.append(result)
                    except IndexError:
                        return False, ()


Review comment (suggestion):
since we filter out `None` below, do we need to check it here? If not, cant we just directly do
```py
                    result_objs.append(next(...))
```

Code after:
                obj = str(obj)

            with contextlib.suppress(IndexError):
                yield obj[key]

    def apply_path(start_obj, path):
        objs = (start_obj,)
        has_branched = False

        for key in variadic(path):
            if is_user_input and key == ':':
                key = ...

            if not casesense and isinstance(key, str):
                key = key.casefold()

            if key is ... or isinstance(key, (list, tuple)) or callable(key):
                has_branched = True

            key_func = functools.partial(apply_key, key)
            objs = itertools.chain.from_iterable(map(key_func, objs))

        return has_branched, objs

    def _traverse_obj(obj, path):
        has_branched, results = apply_path(obj, path)
        results = LazyList(x for x in map(type_test, results) if x is not None)
        if results:
            return results.exhaust() if get_all and has_branched else results[0]

    for path in paths:
        result = _traverse_obj(obj, path)
        if result is not None:
            return result

    return default


def traverse_dict(dictn, keys, casesense=True):
    deprecation_warning(f'"{__name__}.traverse_dict" is deprecated and may be removed '
                        f'in a future version. Use "{__name__}.traverse_obj" instead')
    return traverse_obj(dictn, keys, casesense=casesense, is_user_input=True, traverse_string=True)


def get_first(obj, keys, **kwargs):
    return traverse_obj(obj, (..., *variadic(keys)), **kwargs, get_all=False)


def variadic(x, allowed_types=(str, bytes, dict)):
    return x if isinstance(x, collections.abc.Iterable) and not isinstance(x, allowed_types) else (x,)