Sunday, 25 August 2013

A program to mutate Nested Lists

A program to mutate Nested Lists

Could someone help me do to things:
Review the code and see if it could be written in a better way.
Finish this program. I got stuck in trying to put the list back the way it
is. i.e. a nested list of lists.
Here we go:
t = ['a', 'b', ['c', 'd'], ['e'], 'f']
def capitalize_list(t):
L = []
N = []
for i in range(len(t)):
if type(t[i]) == str:
L.append(t[i].capitalize())
if type(t[i]) == list:
L.extend(t[i])
for s in L:
N.append(s.capitalize())
print N
capitalize_list(t)
This code prints:
['A', 'B', 'C', 'D', 'E', 'F']
I need it to print:
['A', 'B', ['C', 'D'], ['E'], 'F']

No comments:

Post a Comment