Skip to content

Commit b1828a7

Browse files
authored
Initial upload
1 parent 916ef1a commit b1828a7

File tree

2 files changed

+528
-0
lines changed

2 files changed

+528
-0
lines changed

Python List Iteration.ipynb

Lines changed: 197 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,197 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "markdown",
5+
"metadata": {},
6+
"source": [
7+
"# Python List Iteration\n",
8+
"A variety of ways to iterate Lists, including for loop, while loop, enumerate."
9+
]
10+
},
11+
{
12+
"cell_type": "markdown",
13+
"metadata": {},
14+
"source": [
15+
"----\n",
16+
"The standard for loop works great if inside the loop you only need the item and not its index."
17+
]
18+
},
19+
{
20+
"cell_type": "code",
21+
"execution_count": 1,
22+
"metadata": {},
23+
"outputs": [
24+
{
25+
"name": "stdout",
26+
"output_type": "stream",
27+
"text": [
28+
"a\n",
29+
"b\n",
30+
"c\n",
31+
"d\n",
32+
"e\n"
33+
]
34+
}
35+
],
36+
"source": [
37+
"letters = ['a', 'b', 'c', 'd', 'e']\n",
38+
"\n",
39+
"for letter in letters:\n",
40+
" print(letter)"
41+
]
42+
},
43+
{
44+
"cell_type": "markdown",
45+
"metadata": {},
46+
"source": [
47+
"----\n",
48+
"If you need the index inside the loop you can use range(len(list)). \n",
49+
"Then you can always get the list item if needed by using the index."
50+
]
51+
},
52+
{
53+
"cell_type": "code",
54+
"execution_count": 2,
55+
"metadata": {},
56+
"outputs": [
57+
{
58+
"name": "stdout",
59+
"output_type": "stream",
60+
"text": [
61+
"letters 0 = a\n",
62+
"letters 1 = b\n",
63+
"letters 2 = c\n",
64+
"letters 3 = d\n",
65+
"letters 4 = e\n"
66+
]
67+
}
68+
],
69+
"source": [
70+
"for index in range(len(letters)):\n",
71+
" print('letters', index, '=', letters[index])"
72+
]
73+
},
74+
{
75+
"cell_type": "markdown",
76+
"metadata": {},
77+
"source": [
78+
"----\n",
79+
"Best option if you need both index and item inside the loop is to use Python's **enumerate** function. \n",
80+
"Enumerate works in both Python 2.x and 3.x"
81+
]
82+
},
83+
{
84+
"cell_type": "code",
85+
"execution_count": 3,
86+
"metadata": {},
87+
"outputs": [
88+
{
89+
"name": "stdout",
90+
"output_type": "stream",
91+
"text": [
92+
"letters 0 = a\n",
93+
"letters 1 = b\n",
94+
"letters 2 = c\n",
95+
"letters 3 = d\n",
96+
"letters 4 = e\n"
97+
]
98+
}
99+
],
100+
"source": [
101+
"for index, item in enumerate(letters):\n",
102+
" print('letters', index, '=', item)"
103+
]
104+
},
105+
{
106+
"cell_type": "markdown",
107+
"metadata": {},
108+
"source": [
109+
"Enumerate actually returns an iterable enumerate object, \n",
110+
"which is a sequence of tuples of (index, item)."
111+
]
112+
},
113+
{
114+
"cell_type": "code",
115+
"execution_count": 4,
116+
"metadata": {},
117+
"outputs": [
118+
{
119+
"name": "stdout",
120+
"output_type": "stream",
121+
"text": [
122+
"(0, 'a')\n",
123+
"(1, 'b')\n",
124+
"<class 'enumerate'>\n"
125+
]
126+
}
127+
],
128+
"source": [
129+
"enum_obj = enumerate(letters)\n",
130+
"print(next(enum_obj))\n",
131+
"print(next(enum_obj))\n",
132+
"print(type(enum_obj))"
133+
]
134+
},
135+
{
136+
"cell_type": "markdown",
137+
"metadata": {},
138+
"source": [
139+
"----\n",
140+
"Probably the clumsiest way to iterate a list in Python -- the **while loop**. \n",
141+
"Requires index initialization before list, and incrementation inside loop."
142+
]
143+
},
144+
{
145+
"cell_type": "code",
146+
"execution_count": 5,
147+
"metadata": {},
148+
"outputs": [
149+
{
150+
"name": "stdout",
151+
"output_type": "stream",
152+
"text": [
153+
"letters 0 = a\n",
154+
"letters 1 = b\n",
155+
"letters 2 = c\n",
156+
"letters 3 = d\n",
157+
"letters 4 = e\n"
158+
]
159+
}
160+
],
161+
"source": [
162+
"index = 0\n",
163+
"while index < len(letters): \n",
164+
" print('letters', index, '=', letters[index]) \n",
165+
" index += 1"
166+
]
167+
},
168+
{
169+
"cell_type": "code",
170+
"execution_count": null,
171+
"metadata": {},
172+
"outputs": [],
173+
"source": []
174+
}
175+
],
176+
"metadata": {
177+
"kernelspec": {
178+
"display_name": "Python 3",
179+
"language": "python",
180+
"name": "python3"
181+
},
182+
"language_info": {
183+
"codemirror_mode": {
184+
"name": "ipython",
185+
"version": 3
186+
},
187+
"file_extension": ".py",
188+
"mimetype": "text/x-python",
189+
"name": "python",
190+
"nbconvert_exporter": "python",
191+
"pygments_lexer": "ipython3",
192+
"version": "3.7.0"
193+
}
194+
},
195+
"nbformat": 4,
196+
"nbformat_minor": 2
197+
}

0 commit comments

Comments
 (0)