-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDay 14 - flat_list.py
More file actions
41 lines (35 loc) · 909 Bytes
/
Day 14 - flat_list.py
File metadata and controls
41 lines (35 loc) · 909 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
"""
Created on Tue Aug 14 2024
@author: GRACE ESTRADA
Day 14 of 50 Days of Python
Write a function called flat_list that takes one argument, a nested list.
The function converts the nested list into a one-dimension list.
For example [[2,4,5,6]] should return [2,4,5,6]
"""
def flatten_list(nested_list):
flat_list = []
for item in nested_list:
if isinstance(item, list):
flat_list += flatten_list(item) # Recursively flatten the list
else:
flat_list.append(item) # Append the item if it's not a list
return flat_list
list_3d = [
[
[1, 2, 3],
[4, 5, 6],
[7, 8, 9]
],
[
[10, 11, 12],
[13, 14, 15],
[16, 17, 18]
],
[
[19, 20, 21],
[22, 23, 24],
[25, 26, 27]
]
]
flat_list = flatten_list(list_3d)
print(flat_list)