from typing import Any
from typing import Iterator


def iter_nested(data: Any, sortkey: bool=False) -> Iterator[Any]:
  """Iterate over nested data structure
  Note: Use `tuple` instead of `list`. A list type
  object is treated as an item.

  For example:
  >>> data = {'a': (1, 2), 'b': 3}
  >>> list(v for v in iter_nested(data))
  [1, 2, 3]

  Args:
    data (Any): A nested data.
    sortkey (bool): Whether to sort dict's key. Defaults
      to False.
  """
  def _inner_iter_nested(data):
    if isinstance(data, dict):
      keys = sorted(data.keys()) if sortkey else data.keys()
      for k in keys:
        yield from _inner_iter_nested(data[k])
    elif isinstance(data, tuple):
      for v in data:
        yield from _inner_iter_nested(v)
    else:
      yield data
  return _inner_iter_nested(data)