Skip to content

Commit 629994d

Browse files
committed
add new script for makeing list flatten from json input (array)
1 parent 7a7a72e commit 629994d

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed

Scripts/flat_list.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
#!/usr/local/bin/python3
2+
# return flat list from input list
3+
4+
import json
5+
6+
user_input = input('Enter a list with json format ...\n>')
7+
8+
try:
9+
my_list = json.loads(user_input)
10+
except json.decoder.JSONDecodeError:
11+
print('invalid json input. unable to convert inout json to list !!!')
12+
13+
if not isinstance(my_list, list):
14+
print('only list(array) are allowed !!!')
15+
exit(1)
16+
17+
def flat(input_list):
18+
result_list = []
19+
for item in input_list:
20+
if isinstance(item, list):
21+
result_list.extend(flat(item))
22+
else:
23+
result_list.append(item)
24+
return result_list
25+
26+
result = flat(my_list)
27+
print('Input:', my_list)
28+
print('Result:', result)
29+

0 commit comments

Comments
 (0)