Skip to content

Commit 8b65a14

Browse files
committed
How to Create List of Dictionary in Python Using For Loop
1 parent 454d803 commit 8b65a14

4 files changed

+364
-6
lines changed
Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "markdown",
5+
"metadata": {},
6+
"source": [
7+
"## Using dictionary comprehension"
8+
]
9+
},
10+
{
11+
"cell_type": "code",
12+
"execution_count": 1,
13+
"metadata": {},
14+
"outputs": [
15+
{
16+
"name": "stdout",
17+
"output_type": "stream",
18+
"text": [
19+
"{'def_key_gfg': 'gfg', 'def_key_is': 'is', 'def_key_best': 'best'}\n"
20+
]
21+
}
22+
],
23+
"source": [
24+
"a = [\"gfg\", \"is\", \"best\"]\n",
25+
"k = \"def_key_\"\n",
26+
"res = {f\"{k}{ele}\": ele for ele in a}\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": 1,
40+
"metadata": {},
41+
"outputs": [
42+
{
43+
"name": "stdout",
44+
"output_type": "stream",
45+
"text": [
46+
"{'def_keygtg': 'gtg', 'def_keyis': 'is', 'def_keydesi': 'desi'}\n"
47+
]
48+
}
49+
],
50+
"source": [
51+
"a = [\"gtg\", \"is\", \"desi\"]\n",
52+
"k = \"def_key\"\n",
53+
"res = dict(zip(map(lambda x: k+ x,a), a))\n",
54+
"print(res)"
55+
]
56+
},
57+
{
58+
"cell_type": "markdown",
59+
"metadata": {},
60+
"source": [
61+
"## Using dict.fromkeys()"
62+
]
63+
},
64+
{
65+
"cell_type": "code",
66+
"execution_count": 2,
67+
"metadata": {},
68+
"outputs": [
69+
{
70+
"name": "stdout",
71+
"output_type": "stream",
72+
"text": [
73+
"{'dict_key_gfg': 'gfg', 'dict_key_is': 'is', 'dict_key_best': 'best'}\n"
74+
]
75+
}
76+
],
77+
"source": [
78+
"a = [\"gfg\", \"is\", \"best\"]\n",
79+
"k = \"dict_key_\"\n",
80+
"res = dict.fromkeys(map(lambda x:k+x,a))\n",
81+
"for ele in a:\n",
82+
" res[k+ele] = ele\n",
83+
"print(res)"
84+
]
85+
},
86+
{
87+
"cell_type": "markdown",
88+
"metadata": {},
89+
"source": [
90+
"## Using for loop"
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+
"{'def_keygfg': 'gfg', 'def_keyis': 'is', 'def_keybest': 'best'}\n"
103+
]
104+
}
105+
],
106+
"source": [
107+
"a = [\"gfg\", \"is\", \"best\"]\n",
108+
"k = \"def_key\"\n",
109+
"res = {}\n",
110+
"for ele in a:\n",
111+
" res[k + ele] = ele\n",
112+
"print(res)"
113+
]
114+
}
115+
],
116+
"metadata": {
117+
"kernelspec": {
118+
"display_name": "Python 3",
119+
"language": "python",
120+
"name": "python3"
121+
},
122+
"language_info": {
123+
"codemirror_mode": {
124+
"name": "ipython",
125+
"version": 3
126+
},
127+
"file_extension": ".py",
128+
"mimetype": "text/x-python",
129+
"name": "python",
130+
"nbconvert_exporter": "python",
131+
"pygments_lexer": "ipython3",
132+
"version": "3.12.9"
133+
}
134+
},
135+
"nbformat": 4,
136+
"nbformat_minor": 2
137+
}
Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
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": 2,
13+
"metadata": {},
14+
"outputs": [
15+
{
16+
"name": "stdout",
17+
"output_type": "stream",
18+
"text": [
19+
"{8: {'Gfg': 4}, 3: {'is': 5}, 2: {'best': 9}}\n"
20+
]
21+
}
22+
],
23+
"source": [
24+
"a = {\"Gfg\": 4, \"is\": 5, \"best\": 9}\n",
25+
"b = [8,3,2]\n",
26+
"res = {key: {k:v} for key,(k,v) in zip(b,a.items())}\n",
27+
"print(res)"
28+
]
29+
},
30+
{
31+
"cell_type": "markdown",
32+
"metadata": {},
33+
"source": [
34+
"## Using for loop\n"
35+
]
36+
},
37+
{
38+
"cell_type": "code",
39+
"execution_count": 3,
40+
"metadata": {},
41+
"outputs": [
42+
{
43+
"name": "stdout",
44+
"output_type": "stream",
45+
"text": [
46+
"{8: {8: 4}, 3: {3: 5}, 2: {2: 9}}\n"
47+
]
48+
}
49+
],
50+
"source": [
51+
"a = {\"Gfg\": 4, \"is\": 5, \"best\": 9}\n",
52+
"b = [8,3,2]\n",
53+
"res = {}\n",
54+
"for key, (k,v) in zip(b,a.items()):\n",
55+
" res[key] = {key:v}\n",
56+
"print(res)"
57+
]
58+
},
59+
{
60+
"cell_type": "markdown",
61+
"metadata": {},
62+
"source": [
63+
"## Using dict()"
64+
]
65+
},
66+
{
67+
"cell_type": "code",
68+
"execution_count": 5,
69+
"metadata": {},
70+
"outputs": [
71+
{
72+
"name": "stdout",
73+
"output_type": "stream",
74+
"text": [
75+
"{8: {'Gfg': 4}, 3: {'is': 5}, 2: {'best': 9}}\n"
76+
]
77+
}
78+
],
79+
"source": [
80+
"a: dict[str, int] = {\"Gfg\": 4, \"is\": 5, \"best\": 9}\n",
81+
"b: list[int] = [8, 3, 2]\n",
82+
"\n",
83+
"res: dict[int, dict[str, int]] = dict(\n",
84+
" map(lambda key_val: (key_val[0], {key_val[1][0]: key_val[1][1]}), zip(b, a.items()))\n",
85+
")\n",
86+
"print(res)"
87+
]
88+
}
89+
],
90+
"metadata": {
91+
"kernelspec": {
92+
"display_name": "Python 3",
93+
"language": "python",
94+
"name": "python3"
95+
},
96+
"language_info": {
97+
"codemirror_mode": {
98+
"name": "ipython",
99+
"version": 3
100+
},
101+
"file_extension": ".py",
102+
"mimetype": "text/x-python",
103+
"name": "python",
104+
"nbconvert_exporter": "python",
105+
"pygments_lexer": "ipython3",
106+
"version": "3.12.9"
107+
}
108+
},
109+
"nbformat": 4,
110+
"nbformat_minor": 2
111+
}
Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "markdown",
5+
"metadata": {},
6+
"source": [
7+
"## How to Create List of Dictionary in Python Using For Loop"
8+
]
9+
},
10+
{
11+
"cell_type": "markdown",
12+
"metadata": {},
13+
"source": [
14+
"## Using list comprehension"
15+
]
16+
},
17+
{
18+
"cell_type": "code",
19+
"execution_count": 1,
20+
"metadata": {},
21+
"outputs": [
22+
{
23+
"name": "stdout",
24+
"output_type": "stream",
25+
"text": [
26+
"[{'key': 0, 'value': 0}, {'key': 1, 'value': 2}, {'key': 2, 'value': 4}]\n"
27+
]
28+
}
29+
],
30+
"source": [
31+
"a = ['key', 'value']\n",
32+
"b = [dict(zip(a, [i, i*2])) for i in range(3)]\n",
33+
"print(b)"
34+
]
35+
},
36+
{
37+
"cell_type": "markdown",
38+
"metadata": {},
39+
"source": [
40+
"## Using dictionary comprehension"
41+
]
42+
},
43+
{
44+
"cell_type": "code",
45+
"execution_count": 3,
46+
"metadata": {},
47+
"outputs": [
48+
{
49+
"name": "stdout",
50+
"output_type": "stream",
51+
"text": [
52+
"[{'key': 0, 'value': 0}, {'key': 1, 'value': 2}, {'key': 2, 'value': 4}]\n"
53+
]
54+
}
55+
],
56+
"source": [
57+
"a = [\"key\", \"value\"]\n",
58+
"b = [{k: (i if k == \"key\" else i *2) for k in a } for i in range(3)]\n",
59+
"print(b)"
60+
]
61+
},
62+
{
63+
"cell_type": "markdown",
64+
"metadata": {},
65+
"source": [
66+
"## Using append"
67+
]
68+
},
69+
{
70+
"cell_type": "code",
71+
"execution_count": 5,
72+
"metadata": {},
73+
"outputs": [
74+
{
75+
"name": "stdout",
76+
"output_type": "stream",
77+
"text": [
78+
"[{'key': 0, 'value': 0}, {'key': 1, 'value': 2}, {'key': 2, 'value': 4}]\n"
79+
]
80+
}
81+
],
82+
"source": [
83+
"a = ['key', 'value']\n",
84+
"b: list[dict[str, int]] = []\n",
85+
"for i in range(3):\n",
86+
" b.append(dict(zip(a, [i, i*2])))\n",
87+
"print(b)"
88+
]
89+
}
90+
],
91+
"metadata": {
92+
"kernelspec": {
93+
"display_name": "Python 3",
94+
"language": "python",
95+
"name": "python3"
96+
},
97+
"language_info": {
98+
"codemirror_mode": {
99+
"name": "ipython",
100+
"version": 3
101+
},
102+
"file_extension": ".py",
103+
"mimetype": "text/x-python",
104+
"name": "python",
105+
"nbconvert_exporter": "python",
106+
"pygments_lexer": "ipython3",
107+
"version": "3.12.9"
108+
}
109+
},
110+
"nbformat": 4,
111+
"nbformat_minor": 2
112+
}

Array/Python Create Dictionary from List with Default Values.ipynb

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@
6363
},
6464
{
6565
"cell_type": "code",
66-
"execution_count": 5,
66+
"execution_count": null,
6767
"metadata": {},
6868
"outputs": [
6969
{
@@ -75,11 +75,9 @@
7575
}
7676
],
7777
"source": [
78-
"keys: set[str] = {'a','b','c','d'}\n",
79-
"d = 0\n",
80-
"res: dict[str, int] = {\n",
81-
"\n",
82-
"}\n",
78+
"keys: set[str] = {\"a\", \"b\", \"c\", \"d\"}\n",
79+
"d = 0\n",
80+
"res: dict[str, int] = {}\n",
8381
"for key in keys:\n",
8482
" res[key] = 0\n",
8583
"print(res)"

0 commit comments

Comments
 (0)