-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlists.html
More file actions
732 lines (639 loc) · 78.5 KB
/
Copy pathlists.html
File metadata and controls
732 lines (639 loc) · 78.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
<!DOCTYPE html>
<html lang="en" xml:lang="en">
<head>
<meta charset="utf-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<title>Chapter 4 Lists and Sequences | Introduction to Programming</title>
<meta name="description" content="Course reader for IMT/LIS 511: Introduction to Programming for Information and Data Science." />
<meta name="generator" content="bookdown 0.24 and GitBook 2.6.7" />
<meta property="og:title" content="Chapter 4 Lists and Sequences | Introduction to Programming" />
<meta property="og:type" content="book" />
<meta property="og:url" content="https://infx511.github.io/" />
<meta property="og:description" content="Course reader for IMT/LIS 511: Introduction to Programming for Information and Data Science." />
<meta name="github-repo" content="infx511/book" />
<meta name="twitter:card" content="summary" />
<meta name="twitter:title" content="Chapter 4 Lists and Sequences | Introduction to Programming" />
<meta name="twitter:description" content="Course reader for IMT/LIS 511: Introduction to Programming for Information and Data Science." />
<meta name="author" content="Joel Ross" />
<meta name="date" content="2023-07-26" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<meta name="apple-mobile-web-app-capable" content="yes" />
<meta name="apple-mobile-web-app-status-bar-style" content="black" />
<link rel="shortcut icon" href="img/ischool-gold.png" type="image/x-icon" />
<link rel="prev" href="logic.html"/>
<link rel="next" href="loops.html"/>
<script src="libs/header-attrs-2.11/header-attrs.js"></script>
<script src="libs/jquery-3.6.0/jquery-3.6.0.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/fuse.js@6.4.6/dist/fuse.min.js"></script>
<link href="libs/gitbook-2.6.7/css/style.css" rel="stylesheet" />
<link href="libs/gitbook-2.6.7/css/plugin-table.css" rel="stylesheet" />
<link href="libs/gitbook-2.6.7/css/plugin-bookdown.css" rel="stylesheet" />
<link href="libs/gitbook-2.6.7/css/plugin-highlight.css" rel="stylesheet" />
<link href="libs/gitbook-2.6.7/css/plugin-search.css" rel="stylesheet" />
<link href="libs/gitbook-2.6.7/css/plugin-fontsettings.css" rel="stylesheet" />
<link href="libs/gitbook-2.6.7/css/plugin-clipboard.css" rel="stylesheet" />
<link href="libs/anchor-sections-1.0.1/anchor-sections.css" rel="stylesheet" />
<script src="libs/anchor-sections-1.0.1/anchor-sections.js"></script>
<link rel="stylesheet" href="css/style.css" type="text/css" />
<link rel="stylesheet" href="css/prism.min.css" type="text/css" />
</head>
<body>
<div class="book without-animation with-summary font-size-2 font-family-1" data-basepath=".">
<div class="book-summary">
<nav role="navigation">
<ul class="summary">
<li><a href="./" class="title">Introduction to Programming</a></li>
<li class="divider"></li>
<li class="chapter" data-level="" data-path="index.html"><a href="index.html"><i class="fa fa-check"></i>About this Book</a></li>
<li class="part"><span><b>I Python Basics</b></span></li>
<li class="chapter" data-level="1" data-path="python-intro.html"><a href="python-intro.html"><i class="fa fa-check"></i><b>1</b> Introduction to Python</a>
<ul>
<li class="chapter" data-level="1.1" data-path="python-intro.html"><a href="python-intro.html#programming-with-python"><i class="fa fa-check"></i><b>1.1</b> Programming with Python</a>
<ul>
<li class="chapter" data-level="1.1.1" data-path="python-intro.html"><a href="python-intro.html#versions"><i class="fa fa-check"></i><b>1.1.1</b> Versions</a></li>
</ul></li>
<li class="chapter" data-level="1.2" data-path="python-intro.html"><a href="python-intro.html#running-python-code"><i class="fa fa-check"></i><b>1.2</b> Running Python Code</a>
<ul>
<li class="chapter" data-level="1.2.1" data-path="python-intro.html"><a href="python-intro.html#jupyter-notebooks"><i class="fa fa-check"></i><b>1.2.1</b> Jupyter Notebooks</a></li>
<li class="chapter" data-level="1.2.2" data-path="python-intro.html"><a href="python-intro.html#on-the-command-line"><i class="fa fa-check"></i><b>1.2.2</b> On the Command Line</a></li>
</ul></li>
<li class="chapter" data-level="1.3" data-path="python-intro.html"><a href="python-intro.html#comments"><i class="fa fa-check"></i><b>1.3</b> Comments</a></li>
<li class="chapter" data-level="1.4" data-path="python-intro.html"><a href="python-intro.html#variables"><i class="fa fa-check"></i><b>1.4</b> Variables</a>
<ul>
<li class="chapter" data-level="1.4.1" data-path="python-intro.html"><a href="python-intro.html#data-types"><i class="fa fa-check"></i><b>1.4.1</b> Data Types</a></li>
</ul></li>
<li class="chapter" data-level="1.5" data-path="python-intro.html"><a href="python-intro.html#getting-help"><i class="fa fa-check"></i><b>1.5</b> Getting Help</a></li>
<li class="chapter" data-level="" data-path="python-intro.html"><a href="python-intro.html#resources"><i class="fa fa-check"></i>Resources</a></li>
</ul></li>
<li class="chapter" data-level="2" data-path="functions.html"><a href="functions.html"><i class="fa fa-check"></i><b>2</b> Functions</a>
<ul>
<li class="chapter" data-level="2.1" data-path="functions.html"><a href="functions.html#what-are-functions"><i class="fa fa-check"></i><b>2.1</b> What are Functions?</a></li>
<li class="chapter" data-level="2.2" data-path="functions.html"><a href="functions.html#python-function-syntax"><i class="fa fa-check"></i><b>2.2</b> Python Function Syntax</a>
<ul>
<li class="chapter" data-level="2.2.1" data-path="functions.html"><a href="functions.html#object-methods"><i class="fa fa-check"></i><b>2.2.1</b> Object Methods</a></li>
</ul></li>
<li class="chapter" data-level="2.3" data-path="functions.html"><a href="functions.html#built-in-python-functions"><i class="fa fa-check"></i><b>2.3</b> Built-in Python Functions</a>
<ul>
<li class="chapter" data-level="2.3.1" data-path="functions.html"><a href="functions.html#modules-and-libraries"><i class="fa fa-check"></i><b>2.3.1</b> Modules and Libraries</a></li>
</ul></li>
<li class="chapter" data-level="2.4" data-path="functions.html"><a href="functions.html#writing-functions"><i class="fa fa-check"></i><b>2.4</b> Writing Functions</a>
<ul>
<li class="chapter" data-level="2.4.1" data-path="functions.html"><a href="functions.html#doc-strings"><i class="fa fa-check"></i><b>2.4.1</b> Doc Strings</a></li>
</ul></li>
<li class="chapter" data-level="" data-path="functions.html"><a href="functions.html#resources-1"><i class="fa fa-check"></i>Resources</a></li>
</ul></li>
<li class="chapter" data-level="3" data-path="logic.html"><a href="logic.html"><i class="fa fa-check"></i><b>3</b> Logic and Conditionals</a>
<ul>
<li class="chapter" data-level="3.1" data-path="logic.html"><a href="logic.html#booleans"><i class="fa fa-check"></i><b>3.1</b> Booleans</a>
<ul>
<li class="chapter" data-level="3.1.1" data-path="logic.html"><a href="logic.html#relational-operators"><i class="fa fa-check"></i><b>3.1.1</b> Relational Operators</a></li>
<li class="chapter" data-level="3.1.2" data-path="logic.html"><a href="logic.html#boolean-operators"><i class="fa fa-check"></i><b>3.1.2</b> Boolean Operators</a></li>
</ul></li>
<li class="chapter" data-level="3.2" data-path="logic.html"><a href="logic.html#conditional-statements"><i class="fa fa-check"></i><b>3.2</b> Conditional Statements</a>
<ul>
<li class="chapter" data-level="3.2.1" data-path="logic.html"><a href="logic.html#designing-conditions"><i class="fa fa-check"></i><b>3.2.1</b> Designing Conditions</a></li>
</ul></li>
<li class="chapter" data-level="3.3" data-path="logic.html"><a href="logic.html#determining-module-or-script"><i class="fa fa-check"></i><b>3.3</b> Determining Module or Script</a></li>
<li class="chapter" data-level="" data-path="logic.html"><a href="logic.html#resources-2"><i class="fa fa-check"></i>Resources</a></li>
</ul></li>
<li class="chapter" data-level="4" data-path="lists.html"><a href="lists.html"><i class="fa fa-check"></i><b>4</b> Lists and Sequences</a>
<ul>
<li class="chapter" data-level="4.1" data-path="lists.html"><a href="lists.html#what-is-a-list"><i class="fa fa-check"></i><b>4.1</b> What is a List?</a></li>
<li class="chapter" data-level="4.2" data-path="lists.html"><a href="lists.html#list-indices"><i class="fa fa-check"></i><b>4.2</b> List Indices</a></li>
<li class="chapter" data-level="4.3" data-path="lists.html"><a href="lists.html#list-operations-and-methods"><i class="fa fa-check"></i><b>4.3</b> List Operations and Methods</a></li>
<li class="chapter" data-level="4.4" data-path="lists.html"><a href="lists.html#nested-lists"><i class="fa fa-check"></i><b>4.4</b> Nested Lists</a></li>
<li class="chapter" data-level="4.5" data-path="lists.html"><a href="lists.html#other-sequences"><i class="fa fa-check"></i><b>4.5</b> Other Sequences</a>
<ul>
<li class="chapter" data-level="4.5.1" data-path="lists.html"><a href="lists.html#ranges"><i class="fa fa-check"></i><b>4.5.1</b> Ranges</a></li>
<li class="chapter" data-level="4.5.2" data-path="lists.html"><a href="lists.html#tuples"><i class="fa fa-check"></i><b>4.5.2</b> Tuples</a></li>
</ul></li>
<li class="chapter" data-level="" data-path="lists.html"><a href="lists.html#resources-3"><i class="fa fa-check"></i>Resources</a></li>
</ul></li>
<li class="chapter" data-level="5" data-path="loops.html"><a href="loops.html"><i class="fa fa-check"></i><b>5</b> Iterating with Loops</a>
<ul>
<li class="chapter" data-level="5.1" data-path="loops.html"><a href="loops.html#for-loops"><i class="fa fa-check"></i><b>5.1</b> For Loops</a>
<ul>
<li class="chapter" data-level="5.1.1" data-path="loops.html"><a href="loops.html#variables-and-loops"><i class="fa fa-check"></i><b>5.1.1</b> Variables and Loops</a></li>
</ul></li>
<li class="chapter" data-level="5.2" data-path="loops.html"><a href="loops.html#lists-and-loops"><i class="fa fa-check"></i><b>5.2</b> Lists and Loops</a></li>
<li class="chapter" data-level="5.3" data-path="loops.html"><a href="loops.html#nested-loops"><i class="fa fa-check"></i><b>5.3</b> Nested Loops</a></li>
<li class="chapter" data-level="5.4" data-path="loops.html"><a href="loops.html#list-comprehensions"><i class="fa fa-check"></i><b>5.4</b> List Comprehensions</a></li>
<li class="chapter" data-level="5.5" data-path="loops.html"><a href="loops.html#while-loops"><i class="fa fa-check"></i><b>5.5</b> While Loops</a>
<ul>
<li class="chapter" data-level="5.5.1" data-path="loops.html"><a href="loops.html#counting-with-while-loops"><i class="fa fa-check"></i><b>5.5.1</b> Counting with While Loops</a></li>
<li class="chapter" data-level="5.5.2" data-path="loops.html"><a href="loops.html#sentinels"><i class="fa fa-check"></i><b>5.5.2</b> Sentinels</a></li>
<li class="chapter" data-level="5.5.3" data-path="loops.html"><a href="loops.html#difference-between-for-and-while-loops"><i class="fa fa-check"></i><b>5.5.3</b> Difference Between For and While Loops</a></li>
</ul></li>
<li class="chapter" data-level="5.6" data-path="loops.html"><a href="loops.html#iterating-over-files"><i class="fa fa-check"></i><b>5.6</b> Iterating over Files</a>
<ul>
<li class="chapter" data-level="5.6.1" data-path="loops.html"><a href="loops.html#tryexcept"><i class="fa fa-check"></i><b>5.6.1</b> Try/Except</a></li>
</ul></li>
<li class="chapter" data-level="" data-path="loops.html"><a href="loops.html#resources-4"><i class="fa fa-check"></i>Resources</a></li>
</ul></li>
<li class="chapter" data-level="6" data-path="dictionaries.html"><a href="dictionaries.html"><i class="fa fa-check"></i><b>6</b> Dictionaries</a>
<ul>
<li class="chapter" data-level="6.1" data-path="dictionaries.html"><a href="dictionaries.html#what-is-a-dictionary"><i class="fa fa-check"></i><b>6.1</b> What is a Dictionary?</a></li>
<li class="chapter" data-level="6.2" data-path="dictionaries.html"><a href="dictionaries.html#accessing-a-dictionary"><i class="fa fa-check"></i><b>6.2</b> Accessing a Dictionary</a></li>
<li class="chapter" data-level="6.3" data-path="dictionaries.html"><a href="dictionaries.html#dictionary-methods"><i class="fa fa-check"></i><b>6.3</b> Dictionary Methods</a></li>
<li class="chapter" data-level="6.4" data-path="dictionaries.html"><a href="dictionaries.html#nesting-dictionaries"><i class="fa fa-check"></i><b>6.4</b> Nesting Dictionaries</a></li>
<li class="chapter" data-level="6.5" data-path="dictionaries.html"><a href="dictionaries.html#dictionaries-and-loops"><i class="fa fa-check"></i><b>6.5</b> Dictionaries and Loops</a>
<ul>
<li class="chapter" data-level="6.5.1" data-path="dictionaries.html"><a href="dictionaries.html#dictionary-comprehensions"><i class="fa fa-check"></i><b>6.5.1</b> Dictionary Comprehensions</a></li>
</ul></li>
<li class="chapter" data-level="6.6" data-path="dictionaries.html"><a href="dictionaries.html#which-data-structure-do-i-use"><i class="fa fa-check"></i><b>6.6</b> Which Data Structure Do I Use?</a></li>
<li class="chapter" data-level="" data-path="dictionaries.html"><a href="dictionaries.html#resources-5"><i class="fa fa-check"></i>Resources</a></li>
</ul></li>
<li class="chapter" data-level="7" data-path="searching.html"><a href="searching.html"><i class="fa fa-check"></i><b>7</b> Searching and Filtering</a>
<ul>
<li class="chapter" data-level="7.1" data-path="searching.html"><a href="searching.html#linear-search"><i class="fa fa-check"></i><b>7.1</b> Linear Search</a>
<ul>
<li class="chapter" data-level="7.1.1" data-path="searching.html"><a href="searching.html#maximal-search"><i class="fa fa-check"></i><b>7.1.1</b> Maximal Search</a></li>
<li class="chapter" data-level="7.1.2" data-path="searching.html"><a href="searching.html#falsification-search"><i class="fa fa-check"></i><b>7.1.2</b> Falsification Search</a></li>
</ul></li>
<li class="chapter" data-level="7.2" data-path="searching.html"><a href="searching.html#filtering"><i class="fa fa-check"></i><b>7.2</b> Filtering</a></li>
<li class="chapter" data-level="7.3" data-path="searching.html"><a href="searching.html#mapping"><i class="fa fa-check"></i><b>7.3</b> Mapping</a></li>
<li class="chapter" data-level="7.4" data-path="searching.html"><a href="searching.html#search-efficiency"><i class="fa fa-check"></i><b>7.4</b> Search Efficiency</a>
<ul>
<li class="chapter" data-level="7.4.1" data-path="searching.html"><a href="searching.html#linear-search-speed"><i class="fa fa-check"></i><b>7.4.1</b> Linear Search Speed</a></li>
<li class="chapter" data-level="7.4.2" data-path="searching.html"><a href="searching.html#faster-searching-binary-search"><i class="fa fa-check"></i><b>7.4.2</b> Faster Searching: Binary Search</a></li>
<li class="chapter" data-level="7.4.3" data-path="searching.html"><a href="searching.html#slower-algorithms-sorting"><i class="fa fa-check"></i><b>7.4.3</b> Slower Algorithms: Sorting</a></li>
</ul></li>
<li class="chapter" data-level="" data-path="searching.html"><a href="searching.html#resources-6"><i class="fa fa-check"></i>Resources</a></li>
</ul></li>
<li class="chapter" data-level="8" data-path="functional-programming.html"><a href="functional-programming.html"><i class="fa fa-check"></i><b>8</b> Functional Programming</a>
<ul>
<li class="chapter" data-level="8.1" data-path="functional-programming.html"><a href="functional-programming.html#functions-are-variables"><i class="fa fa-check"></i><b>8.1</b> Functions ARE Variables</a>
<ul>
<li class="chapter" data-level="8.1.1" data-path="functional-programming.html"><a href="functional-programming.html#lambdas-anonymous-functions"><i class="fa fa-check"></i><b>8.1.1</b> lambdas: Anonymous Functions</a></li>
</ul></li>
<li class="chapter" data-level="8.2" data-path="functional-programming.html"><a href="functional-programming.html#functional-looping"><i class="fa fa-check"></i><b>8.2</b> Functional Looping</a>
<ul>
<li class="chapter" data-level="8.2.1" data-path="functional-programming.html"><a href="functional-programming.html#map"><i class="fa fa-check"></i><b>8.2.1</b> Map</a></li>
<li class="chapter" data-level="8.2.2" data-path="functional-programming.html"><a href="functional-programming.html#filter"><i class="fa fa-check"></i><b>8.2.2</b> Filter</a></li>
<li class="chapter" data-level="8.2.3" data-path="functional-programming.html"><a href="functional-programming.html#reduce"><i class="fa fa-check"></i><b>8.2.3</b> Reduce</a></li>
</ul></li>
<li class="chapter" data-level="" data-path="functional-programming.html"><a href="functional-programming.html#resources-7"><i class="fa fa-check"></i>Resources</a></li>
</ul></li>
<li class="part"><span><b>II Specific Libraries</b></span></li>
<li class="chapter" data-level="9" data-path="pandas.html"><a href="pandas.html"><i class="fa fa-check"></i><b>9</b> The <code>pandas</code> Library</a>
<ul>
<li class="chapter" data-level="9.1" data-path="pandas.html"><a href="pandas.html#setting-up-pandas"><i class="fa fa-check"></i><b>9.1</b> Setting up <code>pandas</code></a></li>
<li class="chapter" data-level="9.2" data-path="pandas.html"><a href="pandas.html#series"><i class="fa fa-check"></i><b>9.2</b> Series</a>
<ul>
<li class="chapter" data-level="9.2.1" data-path="pandas.html"><a href="pandas.html#series-operations"><i class="fa fa-check"></i><b>9.2.1</b> Series Operations</a></li>
<li class="chapter" data-level="9.2.2" data-path="pandas.html"><a href="pandas.html#series-methods"><i class="fa fa-check"></i><b>9.2.2</b> Series Methods</a></li>
<li class="chapter" data-level="9.2.3" data-path="pandas.html"><a href="pandas.html#series-indexing"><i class="fa fa-check"></i><b>9.2.3</b> Series Indexing</a></li>
</ul></li>
<li class="chapter" data-level="9.3" data-path="pandas.html"><a href="pandas.html#dataframes"><i class="fa fa-check"></i><b>9.3</b> DataFrames</a>
<ul>
<li class="chapter" data-level="9.3.1" data-path="pandas.html"><a href="pandas.html#dataframe-operations"><i class="fa fa-check"></i><b>9.3.1</b> DataFrame Operations</a></li>
<li class="chapter" data-level="9.3.2" data-path="pandas.html"><a href="pandas.html#dataframe-methods"><i class="fa fa-check"></i><b>9.3.2</b> DataFrame Methods</a></li>
<li class="chapter" data-level="9.3.3" data-path="pandas.html"><a href="pandas.html#accessing-dataframes"><i class="fa fa-check"></i><b>9.3.3</b> Accessing DataFrames</a></li>
</ul></li>
<li class="chapter" data-level="9.4" data-path="pandas.html"><a href="pandas.html#grouping"><i class="fa fa-check"></i><b>9.4</b> Grouping</a>
<ul>
<li class="chapter" data-level="9.4.1" data-path="pandas.html"><a href="pandas.html#aggregation"><i class="fa fa-check"></i><b>9.4.1</b> Aggregation</a></li>
</ul></li>
<li class="chapter" data-level="" data-path="pandas.html"><a href="pandas.html#resources-8"><i class="fa fa-check"></i>Resources</a></li>
</ul></li>
<li class="chapter" data-level="10" data-path="accessing-web-apis.html"><a href="accessing-web-apis.html"><i class="fa fa-check"></i><b>10</b> Accessing Web APIs</a>
<ul>
<li class="chapter" data-level="10.1" data-path="accessing-web-apis.html"><a href="accessing-web-apis.html#web-apis"><i class="fa fa-check"></i><b>10.1</b> Web APIs</a></li>
<li class="chapter" data-level="10.2" data-path="accessing-web-apis.html"><a href="accessing-web-apis.html#restful-requests"><i class="fa fa-check"></i><b>10.2</b> RESTful Requests</a>
<ul>
<li class="chapter" data-level="10.2.1" data-path="accessing-web-apis.html"><a href="accessing-web-apis.html#uris"><i class="fa fa-check"></i><b>10.2.1</b> URIs</a></li>
<li class="chapter" data-level="10.2.2" data-path="accessing-web-apis.html"><a href="accessing-web-apis.html#http-verbs"><i class="fa fa-check"></i><b>10.2.2</b> HTTP Verbs</a></li>
</ul></li>
<li class="chapter" data-level="10.3" data-path="accessing-web-apis.html"><a href="accessing-web-apis.html#accessing-web-apis-1"><i class="fa fa-check"></i><b>10.3</b> Accessing Web APIs</a></li>
<li class="chapter" data-level="10.4" data-path="accessing-web-apis.html"><a href="accessing-web-apis.html#json-data"><i class="fa fa-check"></i><b>10.4</b> JSON Data</a></li>
<li class="chapter" data-level="" data-path="accessing-web-apis.html"><a href="accessing-web-apis.html#resources-9"><i class="fa fa-check"></i>Resources</a></li>
</ul></li>
<li class="appendix"><span><b>Appendix</b></span></li>
<li class="chapter" data-level="A" data-path="installing.html"><a href="installing.html"><i class="fa fa-check"></i><b>A</b> Installing Python</a>
<ul>
<li class="chapter" data-level="A.1" data-path="installing.html"><a href="installing.html#python"><i class="fa fa-check"></i><b>A.1</b> Python</a>
<ul>
<li class="chapter" data-level="A.1.1" data-path="installing.html"><a href="installing.html#anaconda"><i class="fa fa-check"></i><b>A.1.1</b> Anaconda</a></li>
</ul></li>
<li class="chapter" data-level="A.2" data-path="installing.html"><a href="installing.html#text-editors"><i class="fa fa-check"></i><b>A.2</b> Text Editors</a>
<ul>
<li class="chapter" data-level="A.2.1" data-path="installing.html"><a href="installing.html#visual-studio-code"><i class="fa fa-check"></i><b>A.2.1</b> Visual Studio Code</a></li>
<li class="chapter" data-level="A.2.2" data-path="installing.html"><a href="installing.html#atom"><i class="fa fa-check"></i><b>A.2.2</b> Atom</a></li>
<li class="chapter" data-level="A.2.3" data-path="installing.html"><a href="installing.html#sublime-text"><i class="fa fa-check"></i><b>A.2.3</b> Sublime Text</a></li>
<li class="chapter" data-level="A.2.4" data-path="installing.html"><a href="installing.html#pycharm"><i class="fa fa-check"></i><b>A.2.4</b> PyCharm</a></li>
</ul></li>
<li class="chapter" data-level="A.3" data-path="installing.html"><a href="installing.html#command-line-tools-bash"><i class="fa fa-check"></i><b>A.3</b> Command Line Tools (Bash)</a>
<ul>
<li class="chapter" data-level="A.3.1" data-path="installing.html"><a href="installing.html#command-line-on-a-mac"><i class="fa fa-check"></i><b>A.3.1</b> Command Line on a Mac</a></li>
<li class="chapter" data-level="A.3.2" data-path="installing.html"><a href="installing.html#command-line-on-windows"><i class="fa fa-check"></i><b>A.3.2</b> Command Line on Windows</a></li>
</ul></li>
<li class="chapter" data-level="" data-path="installing.html"><a href="installing.html#resources-10"><i class="fa fa-check"></i>Resources</a></li>
</ul></li>
<li class="chapter" data-level="B" data-path="markdown.html"><a href="markdown.html"><i class="fa fa-check"></i><b>B</b> Markdown</a>
<ul>
<li class="chapter" data-level="B.1" data-path="markdown.html"><a href="markdown.html#writing-markdown"><i class="fa fa-check"></i><b>B.1</b> Writing Markdown</a>
<ul>
<li class="chapter" data-level="B.1.1" data-path="markdown.html"><a href="markdown.html#text-formatting"><i class="fa fa-check"></i><b>B.1.1</b> Text Formatting</a></li>
<li class="chapter" data-level="B.1.2" data-path="markdown.html"><a href="markdown.html#text-blocks"><i class="fa fa-check"></i><b>B.1.2</b> Text Blocks</a></li>
</ul></li>
<li class="chapter" data-level="B.2" data-path="markdown.html"><a href="markdown.html#rendering-markdown"><i class="fa fa-check"></i><b>B.2</b> Rendering Markdown</a></li>
<li class="chapter" data-level="" data-path="markdown.html"><a href="markdown.html#resources-11"><i class="fa fa-check"></i>Resources</a></li>
</ul></li>
<li class="chapter" data-level="C" data-path="command-line.html"><a href="command-line.html"><i class="fa fa-check"></i><b>C</b> The Command Line</a>
<ul>
<li class="chapter" data-level="C.1" data-path="command-line.html"><a href="command-line.html#accessing-the-command-line"><i class="fa fa-check"></i><b>C.1</b> Accessing the Command line</a></li>
<li class="chapter" data-level="C.2" data-path="command-line.html"><a href="command-line.html#navigating-the-command-line"><i class="fa fa-check"></i><b>C.2</b> Navigating the Command Line</a>
<ul>
<li class="chapter" data-level="C.2.1" data-path="command-line.html"><a href="command-line.html#changing-directories"><i class="fa fa-check"></i><b>C.2.1</b> Changing Directories</a></li>
<li class="chapter" data-level="C.2.2" data-path="command-line.html"><a href="command-line.html#listing-files"><i class="fa fa-check"></i><b>C.2.2</b> Listing Files</a></li>
<li class="chapter" data-level="C.2.3" data-path="command-line.html"><a href="command-line.html#paths"><i class="fa fa-check"></i><b>C.2.3</b> Paths</a></li>
</ul></li>
<li class="chapter" data-level="C.3" data-path="command-line.html"><a href="command-line.html#file-commands"><i class="fa fa-check"></i><b>C.3</b> File Commands</a>
<ul>
<li class="chapter" data-level="C.3.1" data-path="command-line.html"><a href="command-line.html#learning-new-commands"><i class="fa fa-check"></i><b>C.3.1</b> Learning New Commands</a></li>
<li class="chapter" data-level="C.3.2" data-path="command-line.html"><a href="command-line.html#wildcards"><i class="fa fa-check"></i><b>C.3.2</b> Wildcards</a></li>
</ul></li>
<li class="chapter" data-level="C.4" data-path="command-line.html"><a href="command-line.html#dealing-with-errors"><i class="fa fa-check"></i><b>C.4</b> Dealing With Errors</a></li>
<li class="chapter" data-level="C.5" data-path="command-line.html"><a href="command-line.html#directing-output"><i class="fa fa-check"></i><b>C.5</b> Directing Output</a></li>
<li class="chapter" data-level="C.6" data-path="command-line.html"><a href="command-line.html#shell-scripts"><i class="fa fa-check"></i><b>C.6</b> Shell Scripts</a></li>
<li class="chapter" data-level="" data-path="command-line.html"><a href="command-line.html#resources-12"><i class="fa fa-check"></i>Resources</a></li>
</ul></li>
<li class="chapter" data-level="D" data-path="javascript.html"><a href="javascript.html"><i class="fa fa-check"></i><b>D</b> JavaScript</a>
<ul>
<li class="chapter" data-level="D.1" data-path="javascript.html"><a href="javascript.html#programming-with-javascript"><i class="fa fa-check"></i><b>D.1</b> Programming with JavaScript</a>
<ul>
<li class="chapter" data-level="D.1.1" data-path="javascript.html"><a href="javascript.html#history-and-versions"><i class="fa fa-check"></i><b>D.1.1</b> History and Versions</a></li>
<li class="chapter" data-level="D.1.2" data-path="javascript.html"><a href="javascript.html#running-javascript"><i class="fa fa-check"></i><b>D.1.2</b> Running JavaScript</a></li>
</ul></li>
<li class="chapter" data-level="D.2" data-path="javascript.html"><a href="javascript.html#javascript-basics"><i class="fa fa-check"></i><b>D.2</b> JavaScript Basics</a>
<ul>
<li class="chapter" data-level="D.2.1" data-path="javascript.html"><a href="javascript.html#strict-mode"><i class="fa fa-check"></i><b>D.2.1</b> Strict Mode</a></li>
</ul></li>
<li class="chapter" data-level="D.3" data-path="javascript.html"><a href="javascript.html#variables-1"><i class="fa fa-check"></i><b>D.3</b> Variables</a>
<ul>
<li class="chapter" data-level="D.3.1" data-path="javascript.html"><a href="javascript.html#basic-data-types"><i class="fa fa-check"></i><b>D.3.1</b> Basic Data Types</a></li>
<li class="chapter" data-level="D.3.2" data-path="javascript.html"><a href="javascript.html#type-coercion"><i class="fa fa-check"></i><b>D.3.2</b> Type Coercion</a></li>
<li class="chapter" data-level="D.3.3" data-path="javascript.html"><a href="javascript.html#arrays"><i class="fa fa-check"></i><b>D.3.3</b> Arrays</a></li>
<li class="chapter" data-level="D.3.4" data-path="javascript.html"><a href="javascript.html#objects"><i class="fa fa-check"></i><b>D.3.4</b> Objects</a></li>
</ul></li>
<li class="chapter" data-level="D.4" data-path="javascript.html"><a href="javascript.html#control-structures"><i class="fa fa-check"></i><b>D.4</b> Control Structures</a>
<ul>
<li class="chapter" data-level="D.4.1" data-path="javascript.html"><a href="javascript.html#conditionals"><i class="fa fa-check"></i><b>D.4.1</b> Conditionals</a></li>
<li class="chapter" data-level="D.4.2" data-path="javascript.html"><a href="javascript.html#loops-1"><i class="fa fa-check"></i><b>D.4.2</b> Loops</a></li>
</ul></li>
<li class="chapter" data-level="D.5" data-path="javascript.html"><a href="javascript.html#functions-1"><i class="fa fa-check"></i><b>D.5</b> Functions</a>
<ul>
<li class="chapter" data-level="D.5.1" data-path="javascript.html"><a href="javascript.html#functional-programming-1"><i class="fa fa-check"></i><b>D.5.1</b> Functional Programming</a></li>
</ul></li>
<li class="chapter" data-level="" data-path="javascript.html"><a href="javascript.html#resources-13"><i class="fa fa-check"></i>Resources</a></li>
</ul></li>
<li class="divider"></li>
<li><a href="https://github.com/rstudio/bookdown" target="blank">Published with bookdown</a></li>
</ul>
</nav>
</div>
<div class="book-body">
<div class="body-inner">
<div class="book-header" role="navigation">
<h1>
<i class="fa fa-circle-o-notch fa-spin"></i><a href="./">Introduction to Programming</a>
</h1>
</div>
<div class="page-wrapper" tabindex="-1" role="main">
<div class="page-inner">
<section class="normal" id="section-">
<div id="lists" class="section level1" number="4">
<h1><span class="header-section-number">Chapter 4</span> Lists and Sequences</h1>
<p>This chapter will cover using <strong>lists</strong> in Python, which is a data type representing a <em>sequence</em> of data values. A list is a fundamental data type in Python, and key to writing almost all practical programs. Lists are used to store and organize large sets of data (and computer programs usually deal with <em>lots</em> of data). This chapter covers how to create and access lists, as well as a number of related sequence structures. Note in practice, using lists also means using <em>loops</em>, which are covered in the chapter <a href="loops.html#loops"><em>Iterating with Loops</em></a>.</p>
<!--(similar to how a `string` is a sequence of letters, and a `range` is a sequence of numbers) -->
<div id="what-is-a-list" class="section level2" number="4.1">
<h2><span class="header-section-number">4.1</span> What is a List?</h2>
<p>A list is a <strong>mutable</strong>, <strong>ordered</strong> sequence of values that are all stored in a single variable. For example, you can make a list <code>names</code> that contains the strings “Sarah”, “Amit”, and “Zhang”, or a list <code>one_to_seventy</code> that stores the numbers from 1 to 70. Each value in a list is referred to as an <strong>element</strong> of that list; thus your <code>names</code> list would have 3 elements: <code>"Sarah"</code>, <code>"Amit"</code>, and <code>"Zhang"</code>.</p>
<p>Lists are written as literals inside of <em>square brackets</em> (<strong><code>[]</code></strong>), with each element in the list separated by a <em>comma</em> (<strong><code>,</code></strong>):</p>
<pre class="language-python"><code><span class="token comment"># A list of names</span>
names <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token string">"Sarah"</span><span class="token punctuation">,</span> <span class="token string">"Amit"</span><span class="token punctuation">,</span> <span class="token string">"Zhang"</span><span class="token punctuation">]</span>
<span class="token comment"># A list of numbers (lists can contain "duplicate" values)</span>
numbers <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">,</span> <span class="token number">5</span><span class="token punctuation">,</span> <span class="token number">8</span><span class="token punctuation">]</span>
<span class="token comment"># Lists can contain different types of values (including other lists!)</span>
things <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token string">"raindrops"</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token boolean">True</span><span class="token punctuation">,</span> <span class="token punctuation">[</span><span class="token number">5</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">]</span>
<span class="token comment"># Lists can be empty (with no elements)</span>
empty <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token punctuation">]</span></code></pre>
<p class="alert alert-warning">
<strong>Style Requirement</strong>: List variables should be named using <strong>plurals</strong> (name<strong>s</strong>, number<strong>s</strong>, etc.), because lists hold multiple values!
</p>
</div>
<div id="list-indices" class="section level2" number="4.2">
<h2><span class="header-section-number">4.2</span> List Indices</h2>
<p>You can refer to individual elements in a list by their <strong>index</strong>, which is the number of their position in the list. Lists are <em>zero-indexed</em>, which means that positions are counted starting at <code>0</code>. For example, in the list:</p>
<pre class="language-python"><code>vowels <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token string">'a'</span><span class="token punctuation">,</span> <span class="token string">'e'</span><span class="token punctuation">,</span> <span class="token string">'i'</span><span class="token punctuation">,</span> <span class="token string">'o'</span><span class="token punctuation">,</span> <span class="token string">'u'</span><span class="token punctuation">]</span></code></pre>
<p>The <code>'a'</code> (the first element) is at <em>index</em> 0, <code>'e'</code> (the second element) is at index 1, and so on. This also means that the last element can be found at the index <code>length_of_list - 1</code>.</p>
<p>You can retrieve an element from a list using <strong>bracket notation</strong>: you refer to the element at a particular index of a list by writing the name of the list, followed by square brackets (<strong><code>[]</code></strong>) that contain the index of interest:</p>
<pre class="language-python"><code><span class="token comment"># Create the `names` list</span>
names <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token string">"Sarah"</span><span class="token punctuation">,</span> <span class="token string">"Amit"</span><span class="token punctuation">,</span> <span class="token string">"Zhang"</span><span class="token punctuation">]</span>
<span class="token comment"># Access the element at index 0</span>
name_first <span class="token operator">=</span> names<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span>
<span class="token keyword">print</span><span class="token punctuation">(</span>name_first<span class="token punctuation">)</span> <span class="token comment"># Sarah</span>
<span class="token comment"># Access the element at index 2</span>
name_third <span class="token operator">=</span> names<span class="token punctuation">[</span><span class="token number">2</span><span class="token punctuation">]</span>
<span class="token keyword">print</span><span class="token punctuation">(</span>name_third<span class="token punctuation">)</span> <span class="token comment"># Zhang</span>
<span class="token comment"># Accessing an index not in the list will give an error</span>
name_fourth <span class="token operator">=</span> names<span class="token punctuation">[</span><span class="token number">3</span><span class="token punctuation">]</span> <span class="token comment"># IndexError!</span>
<span class="token comment"># Negative indices count backwards from the end</span>
name_last <span class="token operator">=</span> names<span class="token punctuation">[</span><span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">]</span> <span class="token comment"># Zhang</span>
name_second_to_last <span class="token operator">=</span> names<span class="token punctuation">[</span><span class="token operator">-</span><span class="token number">2</span><span class="token punctuation">]</span> <span class="token comment"># Amit</span></code></pre>
<p>The value inside the square brackets can be any expression that resolves to an integer, including variables:</p>
<pre class="language-python"><code><span class="token comment"># Access the element at index `3-2` (1)</span>
names<span class="token punctuation">[</span><span class="token number">3</span><span class="token operator">-</span><span class="token number">2</span><span class="token punctuation">]</span> <span class="token comment"># "Amit"</span>
last_index <span class="token operator">=</span> <span class="token builtin">len</span><span class="token punctuation">(</span>names<span class="token punctuation">)</span> <span class="token operator">-</span> <span class="token number">1</span> <span class="token comment"># last index is length of list - 1</span>
names<span class="token punctuation">[</span>last_index<span class="token punctuation">]</span> <span class="token comment"># "Zhang"</span>
<span class="token comment"># Don't forget to subtract one from the length!</span>
names<span class="token punctuation">[</span><span class="token builtin">len</span><span class="token punctuation">(</span>names<span class="token punctuation">)</span><span class="token punctuation">]</span> <span class="token comment"># IndexError!</span>
<span class="token comment"># Using an index of `-1` is a better way to get the last element</span></code></pre>
<!-- The expression in the brackets will be evaluated first—similar to how arguments to functions are evaluated before being passed in. -->
<p>The most important thing to note about lists is that an indexed reference to a list element (e.g., <code>names[0]</code>) is effectively a <strong>variable in its own right</strong>: you can think of <code>names[0]</code>, <code>names[1]</code>, and <code>names[2]</code> as being equivalent to having variables <code>names_0</code>, <code>names_1</code>, <code>names_2</code> (each of which has its own value). Lists effectively provide a “shortcut” for having lots and lots of variables that are all related; you can “collect” those variables into a list!</p>
<ul>
<li>As a metaphor: you can think of a list as a “hotel” full of values, and each index as the “room number” of that value. For example, the value “Zhang” lives at room #2. The value <code>names[2]</code> refers to “the value that lives in room #2” (similar to how a bellhop might refer to a customer)</li>
</ul>
<p>Because list references are variables, they can be used anywhere that a “normal” variable can be. In particular, this means that variables can be assigned to them, allowing the list to be <strong>mutated</strong> (changed):</p>
<pre class="language-python"><code><span class="token comment"># A list of school supplies</span>
school_supplies <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token string">"Backpack"</span><span class="token punctuation">,</span> <span class="token string">"Laptop"</span><span class="token punctuation">,</span> <span class="token string">"Pen"</span><span class="token punctuation">]</span>
<span class="token comment"># Assign a new value to the element at index 2; replaces "Pen" with "Pencil"</span>
school_supplies<span class="token punctuation">[</span><span class="token number">2</span><span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token string">"Pencil"</span>
<span class="token keyword">print</span><span class="token punctuation">(</span>school_supplies<span class="token punctuation">)</span> <span class="token comment"># ['Backpack', 'Laptop', 'Pencil']</span>
<span class="token comment"># You can only assign values to elements that exist in the list!</span>
school_supplies<span class="token punctuation">[</span><span class="token number">3</span><span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token string">"Paper"</span> <span class="token comment"># IndexError!</span></code></pre>
<ul>
<li>Following the hotel metaphor: <code>names[2] = "Yash"</code> would mean “the value ‘Yash’ is now living in room #2”.</li>
</ul>
<p>Just as with variables: if the list index (e.g., <code>my_list[index]</code>) is on the <em>left</em> side of an assignment, it means the <strong>variable</strong> (which “slot” in the list). If it is on the <em>right</em> side of an assignment, it means the <strong>value</strong> (which element is in that slot).</p>
<pre class="language-python"><code>letters <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token string">'a'</span><span class="token punctuation">,</span> <span class="token string">'b'</span><span class="token punctuation">,</span> <span class="token string">'c'</span><span class="token punctuation">]</span>
letters<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token string">'q'</span> <span class="token comment"># `letters[0]` is the variable we assign to</span>
first_letter <span class="token operator">=</span> letters<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span> <span class="token comment"># `letters[0]` is the value assigned</span></code></pre>
<p>Note that it is also possible to select multiple, <em>consecutive</em> elements from a list by specifying a <strong>slice</strong>. A slice is written as the starting and ending indices separated by a colon (<strong><code>:</code></strong>); the starting index is included and the ending index is <em>excluded</em>. For example:</p>
<pre class="language-python"><code>letters <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token string">'a'</span><span class="token punctuation">,</span> <span class="token string">'b'</span><span class="token punctuation">,</span> <span class="token string">'c'</span><span class="token punctuation">,</span> <span class="token string">'d'</span><span class="token punctuation">,</span> <span class="token string">'e'</span><span class="token punctuation">,</span> <span class="token string">'f'</span><span class="token punctuation">]</span>
<span class="token comment"># Indices 1 through 3 (non-inclusive)</span>
letters<span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">:</span><span class="token number">3</span><span class="token punctuation">]</span> <span class="token comment"># ['b', 'c']</span>
<span class="token comment"># Indices 3 to the end (inclusive)</span>
letters<span class="token punctuation">[</span><span class="token number">3</span><span class="token punctuation">:</span><span class="token punctuation">]</span> <span class="token comment"># ['d', 'e', 'f']</span>
<span class="token comment"># Indices up to 3 (non-inclusive)</span>
letters<span class="token punctuation">[</span><span class="token punctuation">:</span><span class="token number">3</span><span class="token punctuation">]</span> <span class="token comment"># ['a', 'b', 'c']</span>
<span class="token comment"># Indices 2 to (2 from end) (non-inclusive)</span>
letters<span class="token punctuation">[</span><span class="token number">2</span><span class="token punctuation">:</span><span class="token operator">-</span><span class="token number">2</span><span class="token punctuation">]</span> <span class="token comment"># ['c', 'd'], the `e` is excluded</span>
<span class="token comment"># All the indices. This produces a new list with the same contents!</span>
letters<span class="token punctuation">[</span><span class="token punctuation">:</span><span class="token punctuation">]</span></code></pre>
<!-- Add note about third slice argument? Not needed... -->
</div>
<div id="list-operations-and-methods" class="section level2" number="4.3">
<h2><span class="header-section-number">4.3</span> List Operations and Methods</h2>
<p>Lists support a number of different <a href="https://docs.python.org/3/library/stdtypes.html#common-sequence-operations">operations</a> and <a href="https://docs.python.org/3/tutorial/datastructures.html#more-on-lists">methods</a> (functions), some of which are demonstrated below:</p>
<pre class="language-python"><code><span class="token comment"># Use addition (+) to combine lists</span>
<span class="token punctuation">[</span><span class="token string">'a'</span><span class="token punctuation">,</span> <span class="token string">'b'</span><span class="token punctuation">]</span> <span class="token operator">+</span> <span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">]</span> <span class="token comment"># ['a', 'b', 1, 2]</span>
<span class="token comment"># Multiplication (*) performs multiple additions</span>
<span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">]</span> <span class="token operator">*</span> <span class="token number">3</span> <span class="token comment"># [1, 2, 1, 2, 1, 2]</span>
<span class="token comment"># A sample list</span>
letters <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token string">'a'</span><span class="token punctuation">,</span> <span class="token string">'b'</span><span class="token punctuation">,</span> <span class="token string">'c'</span><span class="token punctuation">,</span> <span class="token string">'d'</span><span class="token punctuation">]</span>
<span class="token comment"># Add value to end of list</span>
letters<span class="token punctuation">.</span>append<span class="token punctuation">(</span><span class="token string">'x'</span><span class="token punctuation">)</span> <span class="token comment"># add 'x' at end</span>
<span class="token comment"># Add value in middle of list</span>
letters<span class="token punctuation">.</span>insert<span class="token punctuation">(</span><span class="token number">2</span><span class="token punctuation">,</span> <span class="token string">'y'</span><span class="token punctuation">)</span> <span class="token comment"># put 'y' at index 2 (everyone else shifts over)</span>
<span class="token comment"># Remove from end of list ("pop off")</span>
letters<span class="token punctuation">.</span>pop<span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token comment"># removes and returns the last item (`x`)</span>
<span class="token comment"># Remove from middle</span>
letters<span class="token punctuation">.</span>pop<span class="token punctuation">(</span><span class="token number">2</span><span class="token punctuation">)</span> <span class="token comment"># removes and returns item at index 2 (`y`)</span>
<span class="token comment"># Remove specific value</span>
letters<span class="token punctuation">.</span>remove<span class="token punctuation">(</span><span class="token string">'c'</span><span class="token punctuation">)</span> <span class="token comment"># remove the first 'c' in the list; nothing returned</span>
<span class="token comment"># Remove all elements</span>
letters<span class="token punctuation">.</span>clear<span class="token punctuation">(</span><span class="token punctuation">)</span></code></pre>
<div class="alert alert-warning">
<p>
Note that list methods such as <code>append()</code> or <code>clear()</code> usually <strong>mutate</strong> the existing list value and then return <code>None</code>. In comparison, string methods (such as <code>lower()</code> or <code>replace()</code>) will return a <em>different</em> string value. This is because lists can be changed, but strings cannot (strings are <em>immutable</em>).
</p>
<p>
In general, if you call a list method, you won’t assign the result to a value. But if you call a string method, you will assign the result to a value
</p>
</div>
<p>When comparing lists using a <em>relational operator</em> (e.g., <code>==</code> or <code>></code>), the operation is applied to the lists <strong>member-wise</strong>: the first element in each list is compared, and in the case of a “tie” then the second element in each list are compared, and so on. In practice, this means that (a) you can use <code>==</code> to compare the contents of lists, and (b) a list is “smaller” than another if its first element(s) is smaller than the other’s first element(s).</p>
<p>But be careful: just because two lists have the same contents (are <code>==</code>) does not mean that they are <em>the same list</em>! In particular, two lists can be <em>different objects</em> (values) but still have the same contents. In Python, you can test whether two values are actually <em>the same value</em> (as opposed to having the same contents) using the <strong><code>is</code></strong> operator.</p>
<pre class="language-python"><code><span class="token comment"># With strings, literals are shared (because they cannot be mutated)</span>
str_a <span class="token operator">=</span> <span class="token string">"banana"</span> <span class="token comment"># `str_a` labels string literal "banana"</span>
str_b <span class="token operator">=</span> <span class="token string">"banana"</span> <span class="token comment"># `str_b` labels string literal "banana"</span>
<span class="token comment"># Both variables label the same (literal) value</span>
str_a <span class="token keyword">is</span> str_b <span class="token comment"># True</span>
<span class="token comment"># With lists, each list created is a different object!</span>
list_a <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">,</span><span class="token number">2</span><span class="token punctuation">,</span><span class="token number">3</span><span class="token punctuation">]</span> <span class="token comment"># `list_a` labels a new list [1,2,3]</span>
list_b <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">,</span><span class="token number">2</span><span class="token punctuation">,</span><span class="token number">3</span><span class="token punctuation">]</span> <span class="token comment"># `list_b` labels a new list [1,2,3]</span>
list_a <span class="token operator">==</span> list_b <span class="token comment"># True, have same values as contents</span>
list_a <span class="token keyword">is</span> list_b <span class="token comment"># False, are two different objects</span>
list_c <span class="token operator">=</span> list_a <span class="token comment"># `list_c` labels the value that `list_a` labels</span>
list_a <span class="token keyword">is</span> list_c <span class="token comment"># True, both labels are on the same object</span>
<span class="token comment"># Modify the list!</span>
list_a<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token number">10</span>
<span class="token keyword">print</span><span class="token punctuation">(</span>list_b<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">)</span> <span class="token comment"># 1 (is a different list, so not changed)</span>
<span class="token keyword">print</span><span class="token punctuation">(</span>list_c<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">)</span> <span class="token comment"># 10 (is the same list, so is changed)</span></code></pre>
<p>Keeping track of whether a <em>new</em> list has been created is particularly important when using lists as <strong>arguments to functions</strong>. Function arguments are local variables that are <em>assigned</em> the passed value—if this value is a list, then the assigned variable will refer to the same list, and any modifications to the argument will affect the value outside of the function:</p>
<pre class="language-python"><code><span class="token comment"># A version of a function that modifies the list</span>
<span class="token keyword">def</span> <span class="token function">delete_first</span><span class="token punctuation">(</span>a_list<span class="token punctuation">)</span><span class="token punctuation">:</span>
a_list<span class="token punctuation">.</span>pop<span class="token punctuation">(</span><span class="token number">0</span><span class="token punctuation">)</span> <span class="token comment"># modifies the given value</span>
letters <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token string">'a'</span><span class="token punctuation">,</span><span class="token string">'b'</span><span class="token punctuation">,</span><span class="token string">'c'</span><span class="token punctuation">]</span>
delete_first<span class="token punctuation">(</span>letters<span class="token punctuation">)</span> <span class="token comment"># call function</span>
<span class="token keyword">print</span><span class="token punctuation">(</span>letters<span class="token punctuation">)</span> <span class="token comment"># ['b', 'c'], variable is changed</span>
<span class="token comment"># A version of a function that does not modify the list</span>
<span class="token keyword">def</span> <span class="token function">delete_first_local</span><span class="token punctuation">(</span>a_list<span class="token punctuation">)</span><span class="token punctuation">:</span>
shorter_list <span class="token operator">=</span> a_list<span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">:</span><span class="token punctuation">]</span>
a_list <span class="token operator">=</span> shorter_list <span class="token comment"># creates new local variable (replacing old local var)</span>
letters <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token string">'a'</span><span class="token punctuation">,</span><span class="token string">'b'</span><span class="token punctuation">,</span><span class="token string">'c'</span><span class="token punctuation">]</span>
delete_first_local<span class="token punctuation">(</span>letters<span class="token punctuation">)</span> <span class="token comment"># call function</span>
<span class="token keyword">print</span><span class="token punctuation">(</span>letters<span class="token punctuation">)</span> <span class="token comment"># ['a', 'b', 'c'], variable is not changed</span></code></pre>
</div>
<div id="nested-lists" class="section level2" number="4.4">
<h2><span class="header-section-number">4.4</span> Nested Lists</h2>
<p>As noted at the start of the chapter, lists elements can be of <strong>any</strong> data type (and any <em>combination</em> of data types)—including other lists! These “lists of lists” are known as <strong>nested lists</strong> or <strong>2-dimensional lists</strong> (or <em>3d-lists</em> for a “list of lists of lists”, etc). Nested lists are most commonly used to represent information such as <em>tables</em> or <em>matrices</em>.</p>
<p>Nested lists work exactly like normal lists—the elements just happen to themselves be indexable:</p>
<pre class="language-python"><code><span class="token comment"># A list of different dinners available at a fancy party</span>
<span class="token comment"># This list has 4 elements, each of which is a list of 3 string elements</span>
<span class="token comment"># (the indentation is just for human readability)</span>
dinner_options <span class="token operator">=</span> <span class="token punctuation">[</span>
<span class="token punctuation">[</span><span class="token string">"chicken"</span><span class="token punctuation">,</span> <span class="token string">"mashed potatoes"</span><span class="token punctuation">,</span> <span class="token string">"mixed veggies"</span><span class="token punctuation">]</span><span class="token punctuation">,</span>
<span class="token punctuation">[</span><span class="token string">"steak"</span><span class="token punctuation">,</span> <span class="token string">"seasoned potatoes"</span><span class="token punctuation">,</span> <span class="token string">"asparagus"</span><span class="token punctuation">]</span><span class="token punctuation">,</span>
<span class="token punctuation">[</span><span class="token string">"fish"</span><span class="token punctuation">,</span> <span class="token string">"seasoned rice"</span><span class="token punctuation">,</span> <span class="token string">"green beans"</span><span class="token punctuation">]</span><span class="token punctuation">,</span>
<span class="token punctuation">[</span><span class="token string">"portobello steak"</span><span class="token punctuation">,</span> <span class="token string">"seasoned rice"</span><span class="token punctuation">,</span> <span class="token string">"green beans"</span><span class="token punctuation">]</span>
<span class="token punctuation">]</span>
<span class="token builtin">len</span><span class="token punctuation">(</span>dinner_options<span class="token punctuation">)</span> <span class="token comment"># 4; there are 4 elements in the "outer" list</span>
fish_option <span class="token operator">=</span> dinner_options<span class="token punctuation">[</span><span class="token number">2</span><span class="token punctuation">]</span> <span class="token comment"># ["fish", "seasoned rice", "green beans"]</span>
<span class="token comment"># Because `fish_option` is a list, we can reference its elements by index</span>
<span class="token keyword">print</span><span class="token punctuation">(</span>fish_option<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">)</span> <span class="token comment"># "fish"</span></code></pre>
<p>In this example, <code>fish_option</code> is a variable that refers to a list, and thus its elements can be accessed by index using bracket notation. But as with any operator or function, it is also possible to use bracket notation on an <em>anonymous value</em> (e.g., a literal value that has not been assigned to a variable). That is, because <code>dinner_options[2]</code> is a list, we can use bracket notation refer to an element of that list without assigning it to a variable first:</p>
<pre class="language-python"><code><span class="token comment"># Access the 2th element's 0th element</span>
dinner_options<span class="token punctuation">[</span><span class="token number">2</span><span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span> <span class="token comment"># "fish"</span></code></pre>
<p>This “pair of brackets” notation allows you to easily access elements within nested lists. This is particularly useful for 2d-lists that represent <em>tables</em> as a list of “rows” (often data records), each of which is a list of “column cells” (often data features):</p>
<pre class="language-python"><code><span class="token comment"># A simple table of values</span>
table <span class="token operator">=</span> <span class="token punctuation">[</span> <span class="token punctuation">[</span><span class="token string">'aa'</span><span class="token punctuation">,</span><span class="token string">'ab'</span><span class="token punctuation">,</span><span class="token string">'ac'</span><span class="token punctuation">,</span><span class="token string">'ad'</span><span class="token punctuation">]</span><span class="token punctuation">,</span>
<span class="token punctuation">[</span><span class="token string">'ba'</span><span class="token punctuation">,</span><span class="token string">'bb'</span><span class="token punctuation">,</span><span class="token string">'bc'</span><span class="token punctuation">,</span><span class="token string">'bd'</span><span class="token punctuation">]</span><span class="token punctuation">,</span>
<span class="token punctuation">[</span><span class="token string">'ca'</span><span class="token punctuation">,</span><span class="token string">'cb'</span><span class="token punctuation">,</span><span class="token string">'cc'</span><span class="token punctuation">,</span><span class="token string">'cd'</span><span class="token punctuation">]</span> <span class="token punctuation">]</span>
row_index <span class="token operator">=</span> <span class="token number">1</span> <span class="token comment"># cells starting with 'b'</span>
col_index <span class="token operator">=</span> <span class="token number">3</span> <span class="token comment"># cells ending with 'd'</span>
table<span class="token punctuation">[</span>row_index<span class="token punctuation">]</span><span class="token punctuation">[</span>col_index<span class="token punctuation">]</span> <span class="token comment"># "bd", the cell at (row, col)</span></code></pre>
</div>
<div id="other-sequences" class="section level2" number="4.5">
<h2><span class="header-section-number">4.5</span> Other Sequences</h2>
<p>In Python, lists are the most common example of a <em>sequence</em> data structure. But there are other types of <a href="https://docs.python.org/3/library/stdtypes.html#typesseq">sequences</a> as well.</p>
<p>For example, <strong>strings</strong> are <em>indexed sequences</em> of characters. Thus you can use <em>bracket notation</em> to refer to individual letters, and many string methods make use of the character’s index:</p>
<pre class="language-python"><code><span class="token comment"># Define a string</span>
message_str <span class="token operator">=</span> <span class="token string">"Hello world"</span>
<span class="token comment"># Access individual letters using bracket notation</span>
first_letter <span class="token operator">=</span> message_str<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span>
<span class="token comment"># Can also slice to select multiple letters (substrings)</span>
message_str<span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">:</span><span class="token number">5</span><span class="token punctuation">]</span> <span class="token comment"># "ello"</span>
<span class="token comment"># Use a method find the index of the character 'w'</span>
message_str<span class="token punctuation">.</span>find<span class="token punctuation">(</span><span class="token string">'w'</span><span class="token punctuation">)</span> <span class="token comment"># 6</span></code></pre>
<p>It is possible to convert other sequences (such as strings) into a <code>list</code> variable by using the built-in <code>list()</code> function (similar how you converted numbers into strings with the <code>str()</code> function):</p>
<pre class="language-python"><code><span class="token comment"># Convert a string into a list</span>
<span class="token builtin">list</span><span class="token punctuation">(</span><span class="token string">"hello"</span><span class="token punctuation">)</span> <span class="token comment"># ['h', 'e', 'l', 'l', 'o']</span></code></pre>
<div id="ranges" class="section level3" number="4.5.1">
<h3><span class="header-section-number">4.5.1</span> Ranges</h3>
<p>Another common sequence type is a <a href="https://docs.python.org/3/library/stdtypes.html#ranges"><strong>range</strong></a>. A range represents a sequence of <em>integers</em>. You create a range sequence by using the built-in <code>range()</code> function. This function. The <code>range()</code> function can be called with different arguments depending on the range and spread of numbers you wish to use:</p>
<pre class="language-python"><code><span class="token comment"># One argument produces a range of numbers from 0 to the argument (not inclusive)</span>
<span class="token builtin">range</span><span class="token punctuation">(</span><span class="token number">10</span><span class="token punctuation">)</span> <span class="token comment"># produces `0, 1, 2, 3, 4, 5, 6, 7, 8, 9`</span>
<span class="token comment"># Two arguments produce a range of numbers from the first argument (inclusive) </span>
<span class="token comment"># to the second argument (not inclusive)</span>
<span class="token builtin">range</span><span class="token punctuation">(</span><span class="token number">3</span><span class="token punctuation">,</span><span class="token number">10</span><span class="token punctuation">)</span> <span class="token comment"># produces `3, 4, 5, 6, 7, 8, 9`</span>
<span class="token comment"># Three arguments produces a range of numbers from the first argument (inclusive)</span>
<span class="token comment"># to the second argument (not inclusive), stepping by the third argument</span>
<span class="token builtin">range</span><span class="token punctuation">(</span><span class="token number">0</span><span class="token punctuation">,</span> <span class="token number">10</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">)</span> <span class="token comment"># produces `0, 3, 6, 9`; every 3rd number from 0 to 9</span></code></pre>
<p>The fact that the “ending” argument is non-inclusive is related to how sequences are zero-index. <code>range(10)</code> produces a sequence of 10 values; but because it starts at <code>0</code>, the sequence ends at <code>9</code>. When counting from 0, the 10th number is <code>9</code>. This way a single argument refers to “how many numbers you want”, rather than “what number you will end on”.</p>
<p>You can access individual elements of a range using bracket notation. Note though that ranges are <strong>immutable</strong>, which means you can’t assign a different value to a range element.</p>
<!-- ```python
one_to_five = range(1,6) # remember that the last number is not included!
second_value = one_to_five[2] # 3
one_to_five[2] = 10 # TypeError: 'range' object does not support item assignment
``` -->
<p>Importantly, ranges are different than lists! If you print out a range, you won’t see the whole sequence of numbers. You can convert the range into a list by using the <code>list()</code> function (though again, you don’t commonly want to do this).</p>
<pre class="language-python"><code>one_to_five <span class="token operator">=</span> <span class="token builtin">range</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">,</span><span class="token number">6</span><span class="token punctuation">)</span>
<span class="token keyword">print</span><span class="token punctuation">(</span>one_to_five<span class="token punctuation">)</span> <span class="token comment"># range(1, 6)</span>
one_to_five_list <span class="token operator">=</span> <span class="token builtin">list</span><span class="token punctuation">(</span>one_to_five<span class="token punctuation">)</span>
<span class="token keyword">print</span><span class="token punctuation">(</span>one_to_five_list<span class="token punctuation">)</span> <span class="token comment"># [1, 2, 3, 4, 5]</span></code></pre>
<p>Ranges are most commonly used when doing <em>iteration</em>, as they provide a clean way to specify the number of times you want to do something (or to track which element of a list you’re on). See <a href="loops.html#loops"><em>Iterating with Loops</em></a> for more details on when to use ranges.</p>
</div>
<div id="tuples" class="section level3" number="4.5.2">
<h3><span class="header-section-number">4.5.2</span> Tuples</h3>
<p>While lists are <em>mutable</em> (changeable) sequences of data, <strong>tuples</strong> represent <strong><em>immutable</em></strong> sequences of data. These are useful if you want to enforce that a data value won’t be changed, such as for a function argument (or a dictionary key; see <a href="dictionaries.html#dictionaries">Chapter 10</a>). Indeed, many built-in Python functions utilize tuples.</p>
<p><strong>Tuples</strong> are written as <em>comma-separated sequences</em> of values. They are often placed inside parentheses for clarity (to help indicate the start and end of the tuple values):</p>
<pre class="language-python"><code><span class="token comment"># A tuple with 3 elements</span>
letters_tuple <span class="token operator">=</span> <span class="token punctuation">(</span><span class="token string">'a'</span><span class="token punctuation">,</span> <span class="token string">'b'</span><span class="token punctuation">,</span> <span class="token string">'c'</span><span class="token punctuation">)</span>
<span class="token keyword">print</span><span class="token punctuation">(</span>letters_tuple<span class="token punctuation">)</span> <span class="token comment"># ('a', 'b', 'c')</span>
<span class="token comment"># Also is a tuple with 3 elements (written without parentheses)</span>
numbers_tuple <span class="token operator">=</span> <span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">3</span>
<span class="token keyword">print</span><span class="token punctuation">(</span>numbers_tuple<span class="token punctuation">)</span> <span class="token comment"># (1, 2, 3)</span>
<span class="token comment"># A tuple representing a person's name, age, and whether they are hungry</span>
<span class="token comment"># Tuple values have _implied_ meanings, which should be explained in comments</span>
hungry_person <span class="token operator">=</span> <span class="token punctuation">(</span><span class="token string">'Ada'</span><span class="token punctuation">,</span> <span class="token number">28</span><span class="token punctuation">,</span> <span class="token boolean">True</span><span class="token punctuation">)</span>
<span class="token comment"># In English, tuple variables may be named based on the number of elements</span>
a_triple <span class="token operator">=</span> <span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">,</span><span class="token number">2</span><span class="token punctuation">,</span><span class="token number">3</span><span class="token punctuation">)</span>
a_double <span class="token operator">=</span> <span class="token punctuation">(</span><span class="token number">4</span><span class="token punctuation">,</span><span class="token number">5</span><span class="token punctuation">)</span>
a_single <span class="token operator">=</span> <span class="token punctuation">(</span><span class="token number">6</span><span class="token punctuation">,</span><span class="token punctuation">)</span> <span class="token comment"># extra comma indicates is a tuple, not just the int `6`</span>
empty <span class="token operator">=</span> <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token comment"># an empty expression is a tuple!</span>
<span class="token comment"># For fun</span>
<span class="token builtin">type</span><span class="token punctuation">(</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token comment"># <class 'tuple'> (type of empty expression)</span></code></pre>
<p class="alert alert-warning">
It’s important to note that while you often write tuples in parentheses (and they are printed in parentheses), it is the <strong>commas</strong> that makes a sequence of literals into a tuple. The parentheses act just like like they do in mathematical expressions—they are only necessary to clarify ambiguity in the order-of-operations. You will find that some “idiomatic” expressions using tuples forgo the parentheses, making the syntax look more magical than it is!
</p>
<p>Elements in tuples can be accessed by <strong>index</strong> using <strong>bracket notation</strong>, just like the elements in lists. However, tuples <em>cannot be modified</em>, so you cannot assign a new value to an index in a tuple:</p>
<pre class="language-python"><code><span class="token comment"># A tuple with 3 elements</span>
letter_triple <span class="token operator">=</span> <span class="token punctuation">(</span><span class="token string">'a'</span><span class="token punctuation">,</span><span class="token string">'b'</span><span class="token punctuation">,</span><span class="token string">'c'</span><span class="token punctuation">)</span>
<span class="token comment"># Access with bracket notation</span>
<span class="token keyword">print</span><span class="token punctuation">(</span>letter_triple<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">)</span> <span class="token comment"># 'a'</span>
<span class="token comment"># Can use slices as an index</span>
<span class="token keyword">print</span><span class="token punctuation">(</span>letter_triple<span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">:</span><span class="token number">3</span><span class="token punctuation">]</span><span class="token punctuation">)</span> <span class="token comment"># ('b','c'), a tuple</span>
<span class="token comment"># Cannot assign values to tuple elements</span>
letter_triple<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token string">'q'</span> <span class="token comment"># TypeError!</span></code></pre>
<p>Tuples can be compared using <em>relational operators</em> just like lists, and have the same “member-wise” comparison behavior described above. This makes it easy to order the immutable tuples just like you would order numbers or strings.</p>
<p>Finally, tuples provide one additional useful feature. The Python interpreter uses tuples to perform <strong>multiple assignments</strong>, where you assign multiple values to multiple variables in a single statement. You have already been able to assign multiple, comma-separated values to a single <em>tuple</em> variable (a process called <strong>packing</strong>). But Python also supports having a single sequential value (e.g., a <em>tuple</em>) be assigned to multiple, comma-separate variables (a process called <strong>unpacking</strong>)!</p>
<pre class="language-python"><code><span class="token comment"># A tuple with 3 elements (omitting parentheses)</span>
a_triple <span class="token operator">=</span> <span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">3</span> <span class="token comment"># assign multiple values to single variable (packing)</span>
<span class="token keyword">print</span><span class="token punctuation">(</span>a_triple<span class="token punctuation">)</span> <span class="token comment"># (1, 2, 3)</span>
x<span class="token punctuation">,</span> y<span class="token punctuation">,</span> z <span class="token operator">=</span> <span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">,</span><span class="token number">2</span><span class="token punctuation">,</span><span class="token number">3</span><span class="token punctuation">)</span> <span class="token comment"># assign single tuple value to multiple variables (unpacking)</span>
<span class="token keyword">print</span><span class="token punctuation">(</span>x<span class="token punctuation">)</span> <span class="token comment"># 1</span>
<span class="token keyword">print</span><span class="token punctuation">(</span>y<span class="token punctuation">)</span> <span class="token comment"># 2</span>
<span class="token keyword">print</span><span class="token punctuation">(</span>z<span class="token punctuation">)</span> <span class="token comment"># 3</span>
<span class="token comment"># The VALUE in this statement is evaluated as a tuple, and then is assigned to</span>
<span class="token comment"># multiple variables!</span>
a<span class="token punctuation">,</span> b<span class="token punctuation">,</span> c <span class="token operator">=</span> x<span class="token punctuation">,</span> y<span class="token punctuation">,</span> z <span class="token comment"># a=x; b=y; c=z</span>
<span class="token comment"># The same process can be used to swap values!</span>
a<span class="token punctuation">,</span> b <span class="token operator">=</span> b<span class="token punctuation">,</span> a</code></pre>
<p>This is mostly a useful shortcut (<em>syntactical sugar</em>), but is also used by some idiomatic Python constructions.</p>
</div>
</div>
<div id="resources-3" class="section level2 unnumbered">
<h2>Resources</h2>
<div class="list-condensed">
<ul>
<li><a href="http://openbookproject.net/thinkcs/python/english3e/lists.html">Lists (Severance)</a></li>
<li><a href="https://automatetheboringstuff.com/chapter4/">Lists (Sweigart)</a></li>
<li><a href="https://books.trinket.io/pfe/10-tuples.html">Tuples (Downey)</a></li>
<li><a href="https://docs.python.org/3/library/stdtypes.html#sequence-types-list-tuple-range">Sequence Types (Python Docs)</a></li>
</ul>
</div>
</div>
</div>
</section>
</div>
</div>
</div>
<a href="logic.html" class="navigation navigation-prev " aria-label="Previous page"><i class="fa fa-angle-left"></i></a>
<a href="loops.html" class="navigation navigation-next " aria-label="Next page"><i class="fa fa-angle-right"></i></a>
</div>
</div>
<script src="libs/gitbook-2.6.7/js/app.min.js"></script>
<script src="libs/gitbook-2.6.7/js/clipboard.min.js"></script>
<script src="libs/gitbook-2.6.7/js/plugin-search.js"></script>
<script src="libs/gitbook-2.6.7/js/plugin-sharing.js"></script>
<script src="libs/gitbook-2.6.7/js/plugin-fontsettings.js"></script>
<script src="libs/gitbook-2.6.7/js/plugin-bookdown.js"></script>
<script src="libs/gitbook-2.6.7/js/jquery.highlight.js"></script>
<script src="libs/gitbook-2.6.7/js/plugin-clipboard.js"></script>
<script>
gitbook.require(["gitbook"], function(gitbook) {
gitbook.start({
"sharing": {
"github": true,
"facebook": false,
"twitter": false,
"linkedin": false,
"weibo": false,
"instapaper": false,
"vk": false,
"whatsapp": false,
"all": ["github", "facebook", "twitter", "google"],
"google": false
},
"fontsettings": {
"theme": "white",
"family": "sans",
"size": 2
},
"edit": {
"link": "https://github.com/infx511/book/edit/master/lists.Rmd",
"text": "Edit"
},
"history": {
"link": null,
"text": null
},
"view": {
"link": null,
"text": null
},
"download": null,
"search": {
"engine": "fuse",
"options": null
},
"toc": {
"collapse": "section",
"scroll_highlight": true
}
});
});
</script>
</body>
</html>