Skip to content

Instantly share code, notes, and snippets.

@nikoheikkila
Created March 7, 2012 13:56
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nikoheikkila/1993291 to your computer and use it in GitHub Desktop.
Save nikoheikkila/1993291 to your computer and use it in GitHub Desktop.
Python: Recursive printer for lists
#################################################################
# #
# PRINTR.PY #
# #
# Advanced printing for python terminal or file handling #
# Homepage: http://pypi.python.org/pypi/printr #
# #
#################################################################
import sys
def print_r(the_list, indent=False, level=0, fh=sys.stdout):
'''
@param the_list The list to iterate over
@param indent Option to indent output
@param level Number of levels to iterate
@param fh Output destination, stdout (screen) by default
'''
for each_item in the_list:
if isinstance(each_item, list):
print_r(each_item, indent, level+1, fh)
else:
if indent:
for tab_stop in range(level):
print("\t"*level, end='', file=fh)
print(each_item, file=fh)
# End of file printr.py
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment