Hi.
I'm trying to make a function that takes nested tuples and returns a list of each nested tuple. I managed to make a function that returns the values of the tuples (which are strings), but cannot figure out how to retrieve the 'subtuples' in a list.
Here's what I tried so far:
def getNestedList(nestedtuples):
valuelist = []
for entry in nestedtuples:
if type(entry) is str:
valuelist.extend(entry)
else:
valuelist.extend(getNestedList(entry))
return valuelist
print getNestedList((('1',('2','3')),('4','5')))
Which then returns:
['1', '2', '3', '4', '5']
But my goal is to return something like through recursion:
[['1','2','3'],['2','3'],['4','5'],['1','2','3','4','5']]
I figure this can be done with recursion, but i cannot seem to get a grasp of it. Here is my latest attempt - (I'm rather new to python, so feel free to be amused :) )
def getNestedList(nestedtuples, nestedlist):
valuelist = []
for entry in nestedtuples:
if type(entry) is str:
valuelist.extend(entry)
nestedlist.extend(entry)
else:
valuelist.extend(getNestedList(entry, nestedlist))
nestedlist.extend(getNestedList(entry, nestedlist))
return valuelist, nestedlist
nestedlist = []
getNestedList((('1',('2','3')),('4','5')), nestedlist)
print nestedlist
Any ideas? :)