Skip to content

Commit 4aeddec

Browse files
authored
list comprehensions
an alternative to appending list items
1 parent 8056ee0 commit 4aeddec

File tree

1 file changed

+222
-0
lines changed

1 file changed

+222
-0
lines changed

List comprehension.ipynb

Lines changed: 222 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,222 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "code",
5+
"execution_count": 1,
6+
"metadata": {},
7+
"outputs": [],
8+
"source": [
9+
"a=[1,3,5,7,9,11]"
10+
]
11+
},
12+
{
13+
"cell_type": "markdown",
14+
"metadata": {},
15+
"source": [
16+
"If we want a new list: [2,6,10,14,18,22] that is 2x of each element in list a"
17+
]
18+
},
19+
{
20+
"cell_type": "code",
21+
"execution_count": 2,
22+
"metadata": {},
23+
"outputs": [],
24+
"source": [
25+
"c=[]\n",
26+
"for x in a:\n",
27+
" c.append(x*2)"
28+
]
29+
},
30+
{
31+
"cell_type": "code",
32+
"execution_count": 3,
33+
"metadata": {},
34+
"outputs": [
35+
{
36+
"name": "stdout",
37+
"output_type": "stream",
38+
"text": [
39+
"[2, 6, 10, 14, 18, 22]\n"
40+
]
41+
}
42+
],
43+
"source": [
44+
"print(c)"
45+
]
46+
},
47+
{
48+
"cell_type": "markdown",
49+
"metadata": {},
50+
"source": [
51+
"# List Comprehension"
52+
]
53+
},
54+
{
55+
"cell_type": "code",
56+
"execution_count": 4,
57+
"metadata": {},
58+
"outputs": [],
59+
"source": [
60+
"d=[x*2 for x in a]\n",
61+
"\n",
62+
"#d is a list where each element is x*2 for all x in a"
63+
]
64+
},
65+
{
66+
"cell_type": "code",
67+
"execution_count": 5,
68+
"metadata": {},
69+
"outputs": [
70+
{
71+
"name": "stdout",
72+
"output_type": "stream",
73+
"text": [
74+
"[2, 6, 10, 14, 18, 22]\n"
75+
]
76+
}
77+
],
78+
"source": [
79+
"print(d)"
80+
]
81+
},
82+
{
83+
"cell_type": "markdown",
84+
"metadata": {},
85+
"source": [
86+
"We want to make a new list [1,4,9,16,25,36]"
87+
]
88+
},
89+
{
90+
"cell_type": "code",
91+
"execution_count": 8,
92+
"metadata": {},
93+
"outputs": [],
94+
"source": [
95+
"e=[x**2 for x in range(1,7)]"
96+
]
97+
},
98+
{
99+
"cell_type": "code",
100+
"execution_count": 9,
101+
"metadata": {},
102+
"outputs": [
103+
{
104+
"name": "stdout",
105+
"output_type": "stream",
106+
"text": [
107+
"[1, 4, 9, 16, 25, 36]\n"
108+
]
109+
}
110+
],
111+
"source": [
112+
"print(e)"
113+
]
114+
},
115+
{
116+
"cell_type": "markdown",
117+
"metadata": {},
118+
"source": [
119+
"New list :[36,25,16,9,4,1]"
120+
]
121+
},
122+
{
123+
"cell_type": "code",
124+
"execution_count": 10,
125+
"metadata": {},
126+
"outputs": [
127+
{
128+
"name": "stdout",
129+
"output_type": "stream",
130+
"text": [
131+
"6\n",
132+
"5\n",
133+
"4\n",
134+
"3\n",
135+
"2\n",
136+
"1\n"
137+
]
138+
}
139+
],
140+
"source": [
141+
"for x in range(6,0,-1):\n",
142+
" # the above range function says: define a range starting from 6 upto 0 (excluding 0) and decrementing each by -1\n",
143+
" print(x)"
144+
]
145+
},
146+
{
147+
"cell_type": "code",
148+
"execution_count": 11,
149+
"metadata": {},
150+
"outputs": [],
151+
"source": [
152+
"#list creation...\n",
153+
"f1=[]\n",
154+
"for x in range(6,0,-1):\n",
155+
" f1.append(x**2)"
156+
]
157+
},
158+
{
159+
"cell_type": "code",
160+
"execution_count": 12,
161+
"metadata": {},
162+
"outputs": [
163+
{
164+
"name": "stdout",
165+
"output_type": "stream",
166+
"text": [
167+
"[36, 25, 16, 9, 4, 1]\n"
168+
]
169+
}
170+
],
171+
"source": [
172+
"print(f1)"
173+
]
174+
},
175+
{
176+
"cell_type": "markdown",
177+
"metadata": {},
178+
"source": [
179+
"By List comprehension method:"
180+
]
181+
},
182+
{
183+
"cell_type": "code",
184+
"execution_count": 13,
185+
"metadata": {},
186+
"outputs": [
187+
{
188+
"name": "stdout",
189+
"output_type": "stream",
190+
"text": [
191+
"[36, 25, 16, 9, 4, 1]\n"
192+
]
193+
}
194+
],
195+
"source": [
196+
"f=[x**2 for x in range(6,0,-1)]\n",
197+
"print(f)"
198+
]
199+
}
200+
],
201+
"metadata": {
202+
"kernelspec": {
203+
"display_name": "Python 3",
204+
"language": "python",
205+
"name": "python3"
206+
},
207+
"language_info": {
208+
"codemirror_mode": {
209+
"name": "ipython",
210+
"version": 3
211+
},
212+
"file_extension": ".py",
213+
"mimetype": "text/x-python",
214+
"name": "python",
215+
"nbconvert_exporter": "python",
216+
"pygments_lexer": "ipython3",
217+
"version": "3.7.3"
218+
}
219+
},
220+
"nbformat": 4,
221+
"nbformat_minor": 2
222+
}

0 commit comments

Comments
 (0)