Skip to content

Commit 454d803

Browse files
committed
Python Create Dictionary from List with Default Values
1 parent 486610f commit 454d803

File tree

2 files changed

+230
-0
lines changed

2 files changed

+230
-0
lines changed

Array/Create a List of Floats.ipynb

Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "code",
5+
"execution_count": 1,
6+
"metadata": {},
7+
"outputs": [
8+
{
9+
"name": "stdout",
10+
"output_type": "stream",
11+
"text": [
12+
"[0.1, 2.3, 4.5, 6.7]\n"
13+
]
14+
}
15+
],
16+
"source": [
17+
"a = [0.1,2.3,4.5,6.7]\n",
18+
"print(a)"
19+
]
20+
},
21+
{
22+
"cell_type": "markdown",
23+
"metadata": {},
24+
"source": [
25+
"## Using List Comprehension"
26+
]
27+
},
28+
{
29+
"cell_type": "code",
30+
"execution_count": 2,
31+
"metadata": {},
32+
"outputs": [
33+
{
34+
"name": "stdout",
35+
"output_type": "stream",
36+
"text": [
37+
"[0.0, 0.5, 1.0, 1.5, 2.0]\n"
38+
]
39+
}
40+
],
41+
"source": [
42+
"a: list[float] = [x * 0.5 for x in range(5)]\n",
43+
"print(a)"
44+
]
45+
},
46+
{
47+
"cell_type": "markdown",
48+
"metadata": {},
49+
"source": [
50+
"## Using range() with map and lambda"
51+
]
52+
},
53+
{
54+
"cell_type": "code",
55+
"execution_count": 3,
56+
"metadata": {},
57+
"outputs": [
58+
{
59+
"name": "stdout",
60+
"output_type": "stream",
61+
"text": [
62+
"[0.0, 0.5, 1.0, 1.5, 2.0, 2.5, 3.0, 3.5, 4.0, 4.5]\n"
63+
]
64+
}
65+
],
66+
"source": [
67+
"a = list(map(lambda x: x*0.5, range(10)))\n",
68+
"print(a)\n"
69+
]
70+
},
71+
{
72+
"cell_type": "markdown",
73+
"metadata": {},
74+
"source": [
75+
"## Using numpy (for large lists or more complex operations)"
76+
]
77+
},
78+
{
79+
"cell_type": "code",
80+
"execution_count": 3,
81+
"metadata": {},
82+
"outputs": [
83+
{
84+
"name": "stdout",
85+
"output_type": "stream",
86+
"text": [
87+
"[0. 0.55555556 1.11111111 1.66666667 2.22222222 2.77777778\n",
88+
" 3.33333333 3.88888889 4.44444444 5. ]\n"
89+
]
90+
}
91+
],
92+
"source": [
93+
"import numpy as np\n",
94+
"a = np.linspace(0.0, 5.0,num=10)\n",
95+
"print(a)"
96+
]
97+
}
98+
],
99+
"metadata": {
100+
"kernelspec": {
101+
"display_name": "Python 3",
102+
"language": "python",
103+
"name": "python3"
104+
},
105+
"language_info": {
106+
"codemirror_mode": {
107+
"name": "ipython",
108+
"version": 3
109+
},
110+
"file_extension": ".py",
111+
"mimetype": "text/x-python",
112+
"name": "python",
113+
"nbconvert_exporter": "python",
114+
"pygments_lexer": "ipython3",
115+
"version": "3.12.9"
116+
}
117+
},
118+
"nbformat": 4,
119+
"nbformat_minor": 2
120+
}
Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "markdown",
5+
"metadata": {},
6+
"source": [
7+
"## Using dict.fromkeys()"
8+
]
9+
},
10+
{
11+
"cell_type": "code",
12+
"execution_count": 2,
13+
"metadata": {},
14+
"outputs": [
15+
{
16+
"name": "stdout",
17+
"output_type": "stream",
18+
"text": [
19+
"{'a': 0, 'b': 0, 'c': 0, 'd': 0}\n"
20+
]
21+
}
22+
],
23+
"source": [
24+
"k = ['a', 'b','c','d']\n",
25+
"d=0\n",
26+
"res = dict.fromkeys(k,d)\n",
27+
"print(res)"
28+
]
29+
},
30+
{
31+
"cell_type": "markdown",
32+
"metadata": {},
33+
"source": [
34+
"## Using Dictionary Comprehension"
35+
]
36+
},
37+
{
38+
"cell_type": "code",
39+
"execution_count": 6,
40+
"metadata": {},
41+
"outputs": [
42+
{
43+
"name": "stdout",
44+
"output_type": "stream",
45+
"text": [
46+
"{'a': 0, 'b': 0, 'c': 0}\n"
47+
]
48+
}
49+
],
50+
"source": [
51+
"keys = ['a', 'b', 'c']\n",
52+
"d = 0\n",
53+
"result = {key:d for key in keys}\n",
54+
"print(result)"
55+
]
56+
},
57+
{
58+
"cell_type": "markdown",
59+
"metadata": {},
60+
"source": [
61+
"## Using a for loop"
62+
]
63+
},
64+
{
65+
"cell_type": "code",
66+
"execution_count": 5,
67+
"metadata": {},
68+
"outputs": [
69+
{
70+
"name": "stdout",
71+
"output_type": "stream",
72+
"text": [
73+
"{'b': 0, 'c': 0, 'a': 0, 'd': 0}\n"
74+
]
75+
}
76+
],
77+
"source": [
78+
"keys: set[str] = {'a','b','c','d'}\n",
79+
"d = 0\n",
80+
"res: dict[str, int] = {\n",
81+
"\n",
82+
"}\n",
83+
"for key in keys:\n",
84+
" res[key] = 0\n",
85+
"print(res)"
86+
]
87+
}
88+
],
89+
"metadata": {
90+
"kernelspec": {
91+
"display_name": "Python 3",
92+
"language": "python",
93+
"name": "python3"
94+
},
95+
"language_info": {
96+
"codemirror_mode": {
97+
"name": "ipython",
98+
"version": 3
99+
},
100+
"file_extension": ".py",
101+
"mimetype": "text/x-python",
102+
"name": "python",
103+
"nbconvert_exporter": "python",
104+
"pygments_lexer": "ipython3",
105+
"version": "3.12.9"
106+
}
107+
},
108+
"nbformat": 4,
109+
"nbformat_minor": 2
110+
}

0 commit comments

Comments
 (0)