-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpython1.py
More file actions
22 lines (20 loc) · 778 Bytes
/
python1.py
File metadata and controls
22 lines (20 loc) · 778 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
def flatten(myList, newList):
for element in myList:
if isinstance(element, list): #check type of elements. list or not.
flatten(element, newList) #if it is list, then turn back to flatten function.
else:
newList.append(element) #otherwise add the element to newList.
return newList
print(flatten([[1,'a',['cat'],2],[[[3]],'dog'],4,5], []))
#######################################################################
def reverse_nested_list(myList):
output = []
for element in myList:
if isinstance(element, list):
output.append(reverse_nested_list(element))
else:
output.append(element)
output.reverse()
return output
example = [[1, 2], [3, 4], [5, 6, 7]]
reversed(example)