Skip to content

Commit f0976b4

Browse files
Flattened the nested nested list of values to a single list.
1 parent dd19259 commit f0976b4

File tree

1 file changed

+297
-0
lines changed

1 file changed

+297
-0
lines changed

flattened_nested_list.ipynb

+297
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,297 @@
1+
{
2+
"nbformat": 4,
3+
"nbformat_minor": 0,
4+
"metadata": {
5+
"colab": {
6+
"provenance": [],
7+
"mount_file_id": "1QC8aqJ1jmDgvbL2AcMXxASh61JIgRZdQ",
8+
"authorship_tag": "ABX9TyMvQ6iH7cdBcfbFewmQ7r1w",
9+
"include_colab_link": true
10+
},
11+
"kernelspec": {
12+
"name": "python3",
13+
"display_name": "Python 3"
14+
},
15+
"language_info": {
16+
"name": "python"
17+
}
18+
},
19+
"cells": [
20+
{
21+
"cell_type": "markdown",
22+
"metadata": {
23+
"id": "view-in-github",
24+
"colab_type": "text"
25+
},
26+
"source": [
27+
"<a href=\"https://colab.research.google.com/github/dineshreddy221/Python/blob/main/flattened_nested_list.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
28+
]
29+
},
30+
{
31+
"cell_type": "code",
32+
"source": [
33+
"# flattening the nested list\n",
34+
"l = [1,2,3,['a','b',1],'a',2,[2]]\n",
35+
"#output : [1,2,3,'a','b']\n",
36+
"\n",
37+
"lst = []\n",
38+
"for i in l:\n",
39+
" if type(i) == int:\n",
40+
" lst.append(i)\n",
41+
" elif type(i) == list:\n",
42+
" for elem in i:\n",
43+
" if type(elem) == str:\n",
44+
" lst.append(elem)\n",
45+
" break\n",
46+
"\n",
47+
"print(lst)"
48+
],
49+
"metadata": {
50+
"colab": {
51+
"base_uri": "https://localhost:8080/"
52+
},
53+
"id": "CClOwOo1UES4",
54+
"outputId": "ba3dd8a2-b4e5-4fff-f6db-934cdb42f07f"
55+
},
56+
"execution_count": null,
57+
"outputs": [
58+
{
59+
"output_type": "stream",
60+
"name": "stdout",
61+
"text": [
62+
"[1, 2, 3, 'a', 'b']\n"
63+
]
64+
}
65+
]
66+
},
67+
{
68+
"cell_type": "code",
69+
"source": [
70+
"# separate different data types to list of nested categories\n",
71+
"l_1 = [\"code\", \"meat\",['a','b',1],'a',\"img\",\"stress\"]\n",
72+
"# output :[[\"code\", \"meat\", 'a','b', 'img', 'stress'],[1]]\n",
73+
"\n",
74+
"str_lst = []\n",
75+
"for string in l_1:\n",
76+
" if type(string) == str:\n",
77+
" str_lst.append(string)\n",
78+
" if type(string) == list:\n",
79+
" for elem in string:\n",
80+
" if type(elem) == str:\n",
81+
" str_lst.append(elem)\n",
82+
" else:\n",
83+
" str_lst.insert(len(str_lst), elem)\n",
84+
"print(str_lst)\n"
85+
],
86+
"metadata": {
87+
"id": "hn-lvSUYaXQu",
88+
"colab": {
89+
"base_uri": "https://localhost:8080/"
90+
},
91+
"outputId": "38948868-a816-4147-da0c-94ef1aa0ad91"
92+
},
93+
"execution_count": null,
94+
"outputs": [
95+
{
96+
"output_type": "stream",
97+
"name": "stdout",
98+
"text": [
99+
"['code', 'meat', 'a', 'b', 1, 'a', 'img', 'stress']\n"
100+
]
101+
}
102+
]
103+
},
104+
{
105+
"cell_type": "code",
106+
"source": [
107+
"# nested nested list with integer, strings.\n",
108+
"nested_nested_list = [[1, 2, [3, 4, [5, 6, 7]], 8], ['a', 'b', ['c', 'd', ['e', 'f']]]]\n",
109+
"# output: [1, 2, 3, 4, 5, 6, 7, 8, 'a', 'b', 'c', 'd', 'e', 'f']\n",
110+
"\n",
111+
"lst = []\n",
112+
"for elem in nested_nested_list:\n",
113+
" for lst_elem in elem:\n",
114+
" if type(lst_elem) == int:\n",
115+
" # lst.insert(len(lst), lst_elem)\n",
116+
" lst.append(lst_elem)\n",
117+
" else:\n",
118+
" for i in lst_elem:\n",
119+
" if type(i) == int:\n",
120+
" lst.append(i)\n",
121+
" else:\n",
122+
" if type(i) == list:\n",
123+
" for last_lst in i:\n",
124+
" lst.append(last_lst)\n",
125+
" else:\n",
126+
" if type(i) == str:\n",
127+
" lst.append(i)\n",
128+
"\n",
129+
"print(lst)"
130+
],
131+
"metadata": {
132+
"id": "ywYcDq80afrG",
133+
"colab": {
134+
"base_uri": "https://localhost:8080/"
135+
},
136+
"outputId": "a9c70b23-b25e-4421-9e9b-e8d0931d3dea"
137+
},
138+
"execution_count": null,
139+
"outputs": [
140+
{
141+
"output_type": "stream",
142+
"name": "stdout",
143+
"text": [
144+
"[1, 2, 3, 4, 5, 6, 7, 8, 'a', 'b', 'c', 'd', 'e', 'f']\n"
145+
]
146+
}
147+
]
148+
},
149+
{
150+
"cell_type": "code",
151+
"source": [
152+
"# Nested nested list with integer and string elements:\n",
153+
"nested_nested_list_1 = [[1, 2, [3, 4, ['a', 'b']], 5], ['c', 'd', ['e', 'f', [6, 7, 8]]]]\n",
154+
"# output: [1, 2, 3, 4, 'a', 'b', 5, 'c', 'd', 'e', 'f', 6, 7, 8]\n",
155+
"\n",
156+
"lst_3 = []\n",
157+
"for nes_nes_lst in nested_nested_list_1:\n",
158+
" for nes_lst in nes_nes_lst:\n",
159+
" if type(nes_lst) == int:\n",
160+
" lst_3.append(nes_lst)\n",
161+
" if type(nes_lst) == list:\n",
162+
" for elem in nes_lst:\n",
163+
" if type(elem) == int:\n",
164+
" lst_3.append(elem)\n",
165+
" if type(elem) == str:\n",
166+
" lst_3.append(elem)\n",
167+
" if type(elem) == list:\n",
168+
" for elem_1 in elem:\n",
169+
" lst_3.append(elem_1)\n",
170+
" if type(nes_lst) == str:\n",
171+
" lst_3.append(nes_lst)\n",
172+
"\n",
173+
"print(\"original nested nested list\", nested_nested_list_1)\n",
174+
"\n",
175+
"print(\"list of flatten values\",lst_3)\n"
176+
],
177+
"metadata": {
178+
"colab": {
179+
"base_uri": "https://localhost:8080/"
180+
},
181+
"id": "LONkVGWvdVXw",
182+
"outputId": "841ce724-1312-4a25-e577-be00611b5f07"
183+
},
184+
"execution_count": null,
185+
"outputs": [
186+
{
187+
"output_type": "stream",
188+
"name": "stdout",
189+
"text": [
190+
"original nested nested list [[1, 2, [3, 4, ['a', 'b']], 5], ['c', 'd', ['e', 'f', [6, 7, 8]]]]\n",
191+
"list of flatten values [1, 2, 3, 4, 'a', 'b', 5, 'c', 'd', 'e', 'f', 6, 7, 8]\n"
192+
]
193+
}
194+
]
195+
},
196+
{
197+
"cell_type": "code",
198+
"source": [
199+
"# Nested nested list with boolean and list elements\n",
200+
"nested_nested_list_2 = [[True, False, [True, True, [False, True]], False], [['a', 'b'], ['c', 'd'], [['e'], ['f', 'g']]]]\n",
201+
"# output: [True, False, True, True, False, True, False, 'a', 'b', 'c', 'd', 'e', 'f', 'g']\n",
202+
"\n",
203+
"lst_4 = []\n",
204+
"for nes_nes_lst_2 in nested_nested_list_2:\n",
205+
" for nes_lst_2 in nes_nes_lst_2:\n",
206+
" # print(nes_lst_2)\n",
207+
" if type(nes_lst_2) == bool:\n",
208+
" lst_4.append(nes_lst_2)\n",
209+
" if type(nes_lst_2) == list:\n",
210+
" for nes_elem in nes_lst_2:\n",
211+
" if type(nes_elem) == bool:\n",
212+
" lst_4.append(nes_elem)\n",
213+
" if type(nes_elem) == list:\n",
214+
" for nes_elem_lst in nes_elem:\n",
215+
" lst_4.append(nes_elem_lst)\n",
216+
"\n",
217+
" if type(nes_elem) == str:\n",
218+
" lst_4.append(nes_elem)\n",
219+
"\n",
220+
"print(\"flattened list of values\",lst_4)"
221+
],
222+
"metadata": {
223+
"colab": {
224+
"base_uri": "https://localhost:8080/"
225+
},
226+
"id": "TS8SLnIisbOb",
227+
"outputId": "24c6cca8-bf26-4075-cac8-a1bea255156b"
228+
},
229+
"execution_count": null,
230+
"outputs": [
231+
{
232+
"output_type": "stream",
233+
"name": "stdout",
234+
"text": [
235+
"flattened list of values [True, False, True, True, False, True, False, 'a', 'b', 'c', 'd', 'e', 'f', 'g']\n"
236+
]
237+
}
238+
]
239+
},
240+
{
241+
"cell_type": "code",
242+
"source": [
243+
"# Nested nested list with mixed data types\n",
244+
"nested_nested_list_3 = [['apple', 'banana', [1, 2, 3]], [4, 5, ['cat', 'dog', ['elephant', 6, 7]]]]\n",
245+
"# output: ['apple', 'banana', 1, 2, 3, 4, 5, 'cat', 'dog', 'elephant', 6, 7]\n",
246+
"\n",
247+
"lst_5 = []\n",
248+
"for nes_nes_lst_3 in nested_nested_list_3:\n",
249+
" for nes_lst in nes_nes_lst_3:\n",
250+
" # print(nes_lst)\n",
251+
" if type(nes_lst) == str:\n",
252+
" lst_5.append(nes_lst)\n",
253+
" if type(nes_lst) == int:\n",
254+
" lst_5.append(nes_lst)\n",
255+
" if type(nes_lst) == list:\n",
256+
" for elem in nes_lst:\n",
257+
" if type(elem) == int:\n",
258+
" lst_5.append(elem)\n",
259+
" if type(elem) == str:\n",
260+
" lst_5.append(elem)\n",
261+
" if type(elem) == list:\n",
262+
" for nes_elem in elem:\n",
263+
" lst_5.append(nes_elem)\n",
264+
"\n",
265+
"\n",
266+
"\n",
267+
"print(\"flatened list of values\", lst_5)"
268+
],
269+
"metadata": {
270+
"colab": {
271+
"base_uri": "https://localhost:8080/"
272+
},
273+
"id": "7l7QHe7psbLj",
274+
"outputId": "b28ba1c8-c705-4df9-dbbe-b35ca635bae4"
275+
},
276+
"execution_count": 175,
277+
"outputs": [
278+
{
279+
"output_type": "stream",
280+
"name": "stdout",
281+
"text": [
282+
"flatened list of values ['apple', 'banana', 1, 2, 3, 4, 5, 'cat', 'dog', 'elephant', 6, 7]\n"
283+
]
284+
}
285+
]
286+
},
287+
{
288+
"cell_type": "code",
289+
"source": [],
290+
"metadata": {
291+
"id": "XeJRFj_N41VW"
292+
},
293+
"execution_count": null,
294+
"outputs": []
295+
}
296+
]
297+
}

0 commit comments

Comments
 (0)