Skip to content

Commit 23bb669

Browse files
committed
add Create List of Size n
1 parent 0a55746 commit 23bb669

File tree

4 files changed

+455
-0
lines changed

4 files changed

+455
-0
lines changed

Array/Create List of Size n.ipynb

Lines changed: 161 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,161 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "code",
5+
"execution_count": null,
6+
"metadata": {},
7+
"outputs": [],
8+
"source": [
9+
"n = 5\n",
10+
"a: list[int] = [0] * 5\n",
11+
"print(a)"
12+
]
13+
},
14+
{
15+
"cell_type": "markdown",
16+
"metadata": {},
17+
"source": [
18+
"## Using Liat Comprehension"
19+
]
20+
},
21+
{
22+
"cell_type": "code",
23+
"execution_count": 1,
24+
"metadata": {},
25+
"outputs": [
26+
{
27+
"name": "stdout",
28+
"output_type": "stream",
29+
"text": [
30+
"[0, 0, 0, 0, 0]\n"
31+
]
32+
}
33+
],
34+
"source": [
35+
"n = 5\n",
36+
"a: list[int] = [0 for i in range(n)]\n",
37+
"print(a)"
38+
]
39+
},
40+
{
41+
"cell_type": "markdown",
42+
"metadata": {},
43+
"source": [
44+
"## Using list() Constructor with range()"
45+
]
46+
},
47+
{
48+
"cell_type": "code",
49+
"execution_count": 2,
50+
"metadata": {},
51+
"outputs": [
52+
{
53+
"name": "stdout",
54+
"output_type": "stream",
55+
"text": [
56+
"[0, 1, 2, 3, 4]\n"
57+
]
58+
}
59+
],
60+
"source": [
61+
"n = 5\n",
62+
"a: list[int] = list(range(n))\n",
63+
"print(a)"
64+
]
65+
},
66+
{
67+
"cell_type": "markdown",
68+
"metadata": {},
69+
"source": [
70+
"## Using for loop"
71+
]
72+
},
73+
{
74+
"cell_type": "code",
75+
"execution_count": null,
76+
"metadata": {},
77+
"outputs": [],
78+
"source": [
79+
"n = 5\n",
80+
"a: list[int] = []\n",
81+
"for i in range(n):\n",
82+
" a.append(0)\n",
83+
"print(a)"
84+
]
85+
},
86+
{
87+
"cell_type": "markdown",
88+
"metadata": {},
89+
"source": [
90+
"## Using *args Function"
91+
]
92+
},
93+
{
94+
"cell_type": "code",
95+
"execution_count": 3,
96+
"metadata": {},
97+
"outputs": [
98+
{
99+
"name": "stdout",
100+
"output_type": "stream",
101+
"text": [
102+
"[0, 0, 0, 0, 0]\n"
103+
]
104+
}
105+
],
106+
"source": [
107+
"def create_list(n: int) -> list[int]:\n",
108+
" return [0] * n\n",
109+
"n = 5\n",
110+
"a = create_list(n)\n",
111+
"print(a)"
112+
]
113+
},
114+
{
115+
"cell_type": "code",
116+
"execution_count": 6,
117+
"metadata": {},
118+
"outputs": [
119+
{
120+
"name": "stdout",
121+
"output_type": "stream",
122+
"text": [
123+
"[1, 2, 3, 4]\n"
124+
]
125+
}
126+
],
127+
"source": [
128+
"def create_list(*args: int) -> list[int]:\n",
129+
" return list(args)\n",
130+
"a: list[int] = create_list(1,2,3,4)\n",
131+
"print(a)"
132+
]
133+
},
134+
{
135+
"cell_type": "markdown",
136+
"metadata": {},
137+
"source": []
138+
}
139+
],
140+
"metadata": {
141+
"kernelspec": {
142+
"display_name": "Python 3",
143+
"language": "python",
144+
"name": "python3"
145+
},
146+
"language_info": {
147+
"codemirror_mode": {
148+
"name": "ipython",
149+
"version": 3
150+
},
151+
"file_extension": ".py",
152+
"mimetype": "text/x-python",
153+
"name": "python",
154+
"nbconvert_exporter": "python",
155+
"pygments_lexer": "ipython3",
156+
"version": "3.12.9"
157+
}
158+
},
159+
"nbformat": 4,
160+
"nbformat_minor": 2
161+
}

Array/Create a list of tuple.ipynb

Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "markdown",
5+
"metadata": {},
6+
"source": [
7+
"## Using zip()"
8+
]
9+
},
10+
{
11+
"cell_type": "code",
12+
"execution_count": null,
13+
"metadata": {},
14+
"outputs": [
15+
{
16+
"name": "stdout",
17+
"output_type": "stream",
18+
"text": [
19+
"[(1, 'apple'), (2, 'orange'), (3, 'cherry')]\n"
20+
]
21+
}
22+
],
23+
"source": [
24+
"a = [1, 2, 3]\n",
25+
"b = ['apple', 'orange', 'cherry']\n",
26+
"res: list[tuple[int, str]] = list(zip(a, b))\n",
27+
"print(res)"
28+
]
29+
},
30+
{
31+
"cell_type": "markdown",
32+
"metadata": {},
33+
"source": [
34+
"## Using map()"
35+
]
36+
},
37+
{
38+
"cell_type": "code",
39+
"execution_count": null,
40+
"metadata": {},
41+
"outputs": [
42+
{
43+
"name": "stdout",
44+
"output_type": "stream",
45+
"text": [
46+
"[(1, 'apple'), (2, 'orange'), (3, 'cherry')]\n"
47+
]
48+
}
49+
],
50+
"source": [
51+
"# from collections.abc import Iterator\n",
52+
"a: list[list[int | str]] = [[1, \"apple\"], [2, \"orange\"], [3, \"cherry\"]]\n",
53+
"# res: Iterator[tuple[int | str, ...]] = map(tuple, a)\n",
54+
"res: list[tuple[int | str, ...]] = list(map(tuple, a))\n",
55+
"\n",
56+
"print(res)"
57+
]
58+
},
59+
{
60+
"cell_type": "markdown",
61+
"metadata": {},
62+
"source": [
63+
"## Using list comprehension"
64+
]
65+
},
66+
{
67+
"cell_type": "code",
68+
"execution_count": null,
69+
"metadata": {},
70+
"outputs": [],
71+
"source": [
72+
"a: list[int] = [1,2,3]\n",
73+
"b: list[str] = ['apple', 'orange', 'cherry']\n",
74+
"res: list[tuple[int, str]] = [(x,y) for x,y in zip(a,b)]\n",
75+
"print(res)"
76+
]
77+
},
78+
{
79+
"cell_type": "markdown",
80+
"metadata": {},
81+
"source": [
82+
"## Using for loop"
83+
]
84+
},
85+
{
86+
"cell_type": "code",
87+
"execution_count": null,
88+
"metadata": {},
89+
"outputs": [
90+
{
91+
"name": "stdout",
92+
"output_type": "stream",
93+
"text": [
94+
"[(1, 'apple'), (2, 'orange'), (3, 'cherry')]\n"
95+
]
96+
}
97+
],
98+
"source": [
99+
"a: list[int] = [1,2,3]\n",
100+
"b: list[str] = ['apple', 'orange', 'cherry']\n",
101+
"res: list[tuple[int,str]] = []\n",
102+
"for i in range(len(a)):\n",
103+
" res.append((a[i], b[i]))\n",
104+
"print(res)"
105+
]
106+
}
107+
],
108+
"metadata": {
109+
"kernelspec": {
110+
"display_name": "Python 3",
111+
"language": "python",
112+
"name": "python3"
113+
},
114+
"language_info": {
115+
"codemirror_mode": {
116+
"name": "ipython",
117+
"version": 3
118+
},
119+
"file_extension": ".py",
120+
"mimetype": "text/x-python",
121+
"name": "python",
122+
"nbconvert_exporter": "python",
123+
"pygments_lexer": "ipython3",
124+
"version": "3.12.9"
125+
}
126+
},
127+
"nbformat": 4,
128+
"nbformat_minor": 2
129+
}

0 commit comments

Comments
 (0)