Skip to content

Instantly share code, notes, and snippets.

@rytoj
Last active June 14, 2016 19:14
Show Gist options
  • Save rytoj/40f2a7205fe6cb075fe5dcd37bd65ecb to your computer and use it in GitHub Desktop.
Save rytoj/40f2a7205fe6cb075fe5dcd37bd65ecb to your computer and use it in GitHub Desktop.
nested_sum
#!/usr/bin/python3
T = ["abc",1, 10, [3,2,5], 20, [5,5], "50" ]
newList=[]
def nested_sum(newstedList):
'''
newstedList: list composed of nested lists containing int.
newlist: New flat list composed of list elements.
'''
for i in range(len(newstedList)):
if type(newstedList[i]) == int:
newList.append(newstedList[i])
elif type(newstedList[i]) == str:
continue
else:
nested_sum(newstedList[i])
# print(newList, "Sum:", sum(newList))
return sum(newList)
print(nested_sum(T))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment