0

Say I have a list of lists

L3 = [3, 4, 5]
L2 = [2, L3, 6]
L1 = [1, L2, 7]

Whats the best "Python"-ish way to print L1 without its inner lists showing as lists? (or how to copy all values to a new list of integers)

martineau
  • 119,623
  • 25
  • 170
  • 301
mattC
  • 363
  • 2
  • 17

1 Answers1

1

here a function convert a nesting list to flat list

L3 = [3, 4, 5]
L2 = [2, L3, 6]
L1 = [1, L2, 7]


def flat_list(l):
    result = []
    for item in l:
        if isinstance(item,list):
            result.extend(flat_list(item))
        else:
            result.append(item)
    return result

print flat_list(L1)

#print [1, 2, 3, 4, 5, 6, 7]
shenli
  • 13
  • 3