Skip to content

Instantly share code, notes, and snippets.

@lfalanga
Last active February 17, 2020 03:18
Show Gist options
  • Save lfalanga/91924a2aea489562086236abd98bf6d8 to your computer and use it in GitHub Desktop.
Save lfalanga/91924a2aea489562086236abd98bf6d8 to your computer and use it in GitHub Desktop.
# Example 1: Using recursion to print out a list
lista = ["elemento1n1", "elemento2n1", "elemento3n1",
["elemento1n2", "elemento2n2", "elemento3n2",
["elemento1n3", "elemento2n3", "elemento3n3"]
]
]
# Imprimimos la lista
def recorrer_lista(item):
for x in item:
if isinstance(x, list):
recorrer_lista(x)
else:
print(x)
recorrer_lista(lista)
def recorrer_lista_rev_2(item, nivel=0): # Agrego valor por defecto
for x in item:
if isinstance(x, list):
recorrer_lista_rev_2(x, nivel + 1)
else:
for y in range(nivel):
print("\t", end="") # Agrego indentación en lugar de saltos de línea
print(x)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment