6
I solved the following Python exercise:
Implement a function that receives a list of length lists any and return a list of a dimension.
The solution I was able to make was this::
#!/usr/bin/env python
#-*- coding: utf-8 -*-
def list_unifier(lista_de_listas):
lista_unificada = []
for i in range(0, len(lista_de_listas)):
for j in range(0, len(lista_de_listas[i])):
lista_unificada.append(lista_de_listas[i][j])
return lista_unificada
print list_unifier([[1, 2], [3, 4, 5], [], [6, 7]])
Exit:
[1, 2, 3, 4, 5, 6, 7]
The question is, would this be the best way to do it? I found it a little long.
I was thinking something involving for i in lista_de_listas
or something like that.
Several examples here: https://stackoverflow.com/questions/952914/making-a-flat-list-out-of-list-of-lists-in-python
– bruno
@Bruno Missed specify on question but it would be something without any advanced feature, only basic flow control. I’m still on language ABC. :)
– Piovezan