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
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "cells": [
3
+ {
4
+ "cell_type": "markdown",
5
+ "metadata": {
6
+ "id": "winJJbiUiIIO"
7
+ },
8
+ "source": [
9
+ "Data structures: Dictionaries"
10
+ ]
11
+ },
12
+ {
13
+ "cell_type": "code",
14
+ "execution_count": 1,
15
+ "metadata": {
16
+ "id": "yih6CE4RiIrl"
17
+ },
18
+ "outputs": [],
19
+ "source": [
20
+ "Dictionaries = {\"key1\":\"value1\"}"
21
+ ]
22
+ },
23
+ {
24
+ "cell_type": "markdown",
25
+ "metadata": {
26
+ "id": "56RkusxohJDa"
27
+ },
28
+ "source": [
29
+ "Create Dictionaries"
30
+ ]
31
+ },
32
+ {
33
+ "cell_type": "code",
34
+ "execution_count": 2,
35
+ "metadata": {
36
+ "id": "VLkxduR8hSQ_"
37
+ },
38
+ "outputs": [],
39
+ "source": [
40
+ "dic = {\n",
41
+ " \"brand\": \"Ford\",\n",
42
+ " \"model\": \"Mustang\",\n",
43
+ " \"year\": 1964\n",
44
+ "}"
45
+ ]
46
+ },
47
+ {
48
+ "cell_type": "code",
49
+ "execution_count": 3,
50
+ "metadata": {
51
+ "colab": {
52
+ "base_uri": "https://localhost:8080/"
53
+ },
54
+ "id": "Ihvoijply3U5",
55
+ "outputId": "9f69778d-96a9-40b7-c1b9-b9feb4b0bc6d"
56
+ },
57
+ "outputs": [
58
+ {
59
+ "name": "stdout",
60
+ "output_type": "stream",
61
+ "text": [
62
+ "{'brand': 'Ford', 'model': 'Mustang', 'year': 1964}\n"
63
+ ]
64
+ }
65
+ ],
66
+ "source": [
67
+ "print(dic)"
68
+ ]
69
+ },
70
+ {
71
+ "cell_type": "code",
72
+ "execution_count": 4,
73
+ "metadata": {
74
+ "colab": {
75
+ "base_uri": "https://localhost:8080/"
76
+ },
77
+ "id": "dCW1nOWDiNRW",
78
+ "outputId": "693b217a-6e52-4b7c-f054-9c34119daa31"
79
+ },
80
+ "outputs": [
81
+ {
82
+ "name": "stdout",
83
+ "output_type": "stream",
84
+ "text": [
85
+ "{'brand': 'Ford', 'model': 'Mustang', 'year': 1964}\n",
86
+ "3\n",
87
+ "<class 'dict'>\n"
88
+ ]
89
+ }
90
+ ],
91
+ "source": [
92
+ "print(dic)\n",
93
+ "print(len(dic))\n",
94
+ "print(type(dic))"
95
+ ]
96
+ },
97
+ {
98
+ "cell_type": "code",
99
+ "execution_count": 5,
100
+ "metadata": {
101
+ "colab": {
102
+ "base_uri": "https://localhost:8080/"
103
+ },
104
+ "id": "cOUjZZdviN0G",
105
+ "outputId": "f2c4422e-453a-4f72-9bf0-f699c6339afd"
106
+ },
107
+ "outputs": [
108
+ {
109
+ "name": "stdout",
110
+ "output_type": "stream",
111
+ "text": [
112
+ "{'brand': 'Ford', 'model': 'Mustang', 'year': 1964}\n"
113
+ ]
114
+ }
115
+ ],
116
+ "source": [
117
+ "dic = dict(brand = \"Ford\", model = \"Mustang\", year = 1964)\n",
118
+ "print(dic)"
119
+ ]
120
+ },
121
+ {
122
+ "cell_type": "markdown",
123
+ "metadata": {
124
+ "id": "k95vij2nkKLS"
125
+ },
126
+ "source": [
127
+ "Accessing Items"
128
+ ]
129
+ },
130
+ {
131
+ "cell_type": "code",
132
+ "execution_count": 6,
133
+ "metadata": {
134
+ "colab": {
135
+ "base_uri": "https://localhost:8080/"
136
+ },
137
+ "id": "Pq5DQvKGkJO7",
138
+ "outputId": "e5e8474c-75b8-4e59-dd4a-6c81c92901a6"
139
+ },
140
+ "outputs": [
141
+ {
142
+ "name": "stdout",
143
+ "output_type": "stream",
144
+ "text": [
145
+ "1964\n"
146
+ ]
147
+ }
148
+ ],
149
+ "source": [
150
+ "x = dic[\"year\"]\n",
151
+ "print(x)"
152
+ ]
153
+ },
154
+ {
155
+ "cell_type": "code",
156
+ "execution_count": 7,
157
+ "metadata": {
158
+ "colab": {
159
+ "base_uri": "https://localhost:8080/",
160
+ "height": 35
161
+ },
162
+ "id": "7NrgctylkR0D",
163
+ "outputId": "a1c24afa-80c7-4535-fda4-796de9986b9f"
164
+ },
165
+ "outputs": [
166
+ {
167
+ "data": {
168
+ "text/plain": [
169
+ "'Mustang'"
170
+ ]
171
+ },
172
+ "execution_count": 7,
173
+ "metadata": {},
174
+ "output_type": "execute_result"
175
+ }
176
+ ],
177
+ "source": [
178
+ "x = dic.get(\"model\")\n",
179
+ "x"
180
+ ]
181
+ },
182
+ {
183
+ "cell_type": "markdown",
184
+ "metadata": {
185
+ "id": "aKSZoGH-iYTm"
186
+ },
187
+ "source": [
188
+ "keys()"
189
+ ]
190
+ },
191
+ {
192
+ "cell_type": "code",
193
+ "execution_count": 8,
194
+ "metadata": {
195
+ "colab": {
196
+ "base_uri": "https://localhost:8080/"
197
+ },
198
+ "id": "t9pG91qkkYDk",
199
+ "outputId": "156acb1f-8197-4799-c6db-8cf4dc05a5b4"
200
+ },
201
+ "outputs": [
202
+ {
203
+ "name": "stdout",
204
+ "output_type": "stream",
205
+ "text": [
206
+ "dict_keys(['brand', 'model', 'year'])\n"
207
+ ]
208
+ }
209
+ ],
210
+ "source": [
211
+ "x = dic.keys()\n",
212
+ "print(x)"
213
+ ]
214
+ },
215
+ {
216
+ "cell_type": "code",
217
+ "execution_count": 9,
218
+ "metadata": {
219
+ "colab": {
220
+ "base_uri": "https://localhost:8080/"
221
+ },
222
+ "id": "XIY6AocbifhJ",
223
+ "outputId": "ce5bbca4-2808-4d80-bfa2-91eb5321e8d4"
224
+ },
225
+ "outputs": [
226
+ {
227
+ "data": {
228
+ "text/plain": [
229
+ "dict_values(['Ford', 'Mustang', 1964])"
230
+ ]
231
+ },
232
+ "execution_count": 9,
233
+ "metadata": {},
234
+ "output_type": "execute_result"
235
+ }
236
+ ],
237
+ "source": [
238
+ "x = dic.values()\n",
239
+ "x"
240
+ ]
241
+ },
242
+ {
243
+ "cell_type": "code",
244
+ "execution_count": 10,
245
+ "metadata": {
246
+ "colab": {
247
+ "base_uri": "https://localhost:8080/"
248
+ },
249
+ "id": "r3UjUOwMir1v",
250
+ "outputId": "e9946a9b-d7e8-4e58-8e06-8772b0ca87ee"
251
+ },
252
+ "outputs": [
253
+ {
254
+ "data": {
255
+ "text/plain": [
256
+ "dict_items([('brand', 'Ford'), ('model', 'Mustang'), ('year', 1964)])"
257
+ ]
258
+ },
259
+ "execution_count": 10,
260
+ "metadata": {},
261
+ "output_type": "execute_result"
262
+ }
263
+ ],
264
+ "source": [
265
+ "x = dic.items()\n",
266
+ "x"
267
+ ]
268
+ },
269
+ {
270
+ "cell_type": "markdown",
271
+ "metadata": {
272
+ "id": "QNjHr3FxhhrI"
273
+ },
274
+ "source": [
275
+ "Add Items"
276
+ ]
277
+ },
278
+ {
279
+ "cell_type": "code",
280
+ "execution_count": 11,
281
+ "metadata": {
282
+ "colab": {
283
+ "base_uri": "https://localhost:8080/"
284
+ },
285
+ "id": "P-jX07npkdWt",
286
+ "outputId": "41980110-ea06-4eec-f3ab-864c32906360"
287
+ },
288
+ "outputs": [
289
+ {
290
+ "data": {
291
+ "text/plain": [
292
+ "{'brand': 'Ford', 'model': 'Mustang', 'year': 1964, 'color': 'white'}"
293
+ ]
294
+ },
295
+ "execution_count": 11,
296
+ "metadata": {},
297
+ "output_type": "execute_result"
298
+ }
299
+ ],
300
+ "source": [
301
+ "dic[\"color\"] = \"white\"\n",
302
+ "dic"
303
+ ]
304
+ },
305
+ {
306
+ "cell_type": "code",
307
+ "execution_count": 12,
308
+ "metadata": {
309
+ "colab": {
310
+ "base_uri": "https://localhost:8080/"
311
+ },
312
+ "id": "df2cHHGInqBE",
313
+ "outputId": "6c8a7c34-fad7-4075-ca3e-26fb94818e55"
314
+ },
315
+ "outputs": [
316
+ {
317
+ "data": {
318
+ "text/plain": [
319
+ "{'brand': 'Ford',\n",
320
+ " 'model': 'Mustang',\n",
321
+ " 'year': 1964,\n",
322
+ " 'color': 'white',\n",
323
+ " 'Power': '315 HP'}"
324
+ ]
325
+ },
326
+ "execution_count": 12,
327
+ "metadata": {},
328
+ "output_type": "execute_result"
329
+ }
330
+ ],
331
+ "source": [
332
+ "d= {\"Power\":\"315 HP\"}\n",
333
+ "dic.update(d)\n",
334
+ "dic"
335
+ ]
336
+ },
337
+ {
338
+ "cell_type": "markdown",
339
+ "metadata": {
340
+ "id": "MQQ-FXNhi1sm"
341
+ },
342
+ "source": [
343
+ "Modify"
344
+ ]
345
+ },
346
+ {
347
+ "cell_type": "code",
348
+ "execution_count": 13,
349
+ "metadata": {
350
+ "colab": {
351
+ "base_uri": "https://localhost:8080/"
352
+ },
353
+ "id": "vTI0Js9Qk4i3",
354
+ "outputId": "fedbb0b2-c422-44dd-96a6-0e737cc1c601"
355
+ },
356
+ "outputs": [
357
+ {
358
+ "name": "stdout",
359
+ "output_type": "stream",
360
+ "text": [
361
+ "dict_values(['Ford', 'Mustang', 1964, 'white', '315 HP'])\n",
362
+ "dict_values(['Ford', 'Mustang', 2020, 'white', '315 HP'])\n"
363
+ ]
364
+ }
365
+ ],
366
+ "source": [
367
+ "x = dic.values()\n",
368
+ "\n",
369
+ "print(x) #before the change\n",
370
+ "\n",
371
+ "dic[\"year\"] = 2020\n",
372
+ "\n",
373
+ "print(x) #after the change"
374
+ ]
375
+ },
376
+ {
377
+ "cell_type": "code",
378
+ "execution_count": 14,
379
+ "metadata": {
380
+ "colab": {
381
+ "base_uri": "https://localhost:8080/"
382
+ },
383
+ "id": "uvPL_HxuoeYj",
384
+ "outputId": "26100336-0df1-475e-fe1d-43aa96310b02"
385
+ },
386
+ "outputs": [
387
+ {
388
+ "data": {
389
+ "text/plain": [
390
+ "{'brand': 'Ford',\n",
391
+ " 'model': 'Mustang',\n",
392
+ " 'year': 2020,\n",
393
+ " 'color': 'white',\n",
394
+ " 'Power': '400 HP'}"
395
+ ]
396
+ },
397
+ "execution_count": 14,
398
+ "metadata": {},
399
+ "output_type": "execute_result"
400
+ }
401
+ ],
402
+ "source": [
403
+ "dic.update({\"Power\":\"400 HP\"})\n",
404
+ "dic"
405
+ ]
406
+ },
407
+ {
408
+ "cell_type": "markdown",
409
+ "metadata": {
410
+ "id": "0H-RH2KAk01h"
411
+ },
412
+ "source": [
413
+ "IF condition on dictionary"
414
+ ]
415
+ },
416
+ {
417
+ "cell_type": "code",
418
+ "execution_count": 15,
419
+ "metadata": {
420
+ "colab": {
421
+ "base_uri": "https://localhost:8080/"
422
+ },
423
+ "id": "RIKmYHRslRkZ",
424
+ "outputId": "25a74495-4db4-4dce-c157-f15ad5d609cf"
425
+ },
426
+ "outputs": [
427
+ {
428
+ "name": "stdout",
429
+ "output_type": "stream",
430
+ "text": [
431
+ "Mustang\n"
432
+ ]
433
+ }
434
+ ],
435
+ "source": [
436
+ "if \"model\" in dic:\n",
437
+ " print(dic[\"model\"])"
438
+ ]
439
+ },
440
+ {
441
+ "cell_type": "markdown",
442
+ "metadata": {
443
+ "id": "aHCU62mgl42t"
444
+ },
445
+ "source": [
446
+ "Remove"
447
+ ]
448
+ },
449
+ {
450
+ "cell_type": "code",
451
+ "execution_count": 16,
452
+ "metadata": {
453
+ "colab": {
454
+ "base_uri": "https://localhost:8080/"
455
+ },
456
+ "id": "Tt7MiZJ4lbX6",
457
+ "outputId": "fb951324-8c71-4bf4-da40-c260cbcae871"
458
+ },
459
+ "outputs": [
460
+ {
461
+ "name": "stdout",
462
+ "output_type": "stream",
463
+ "text": [
464
+ "{'brand': 'Ford', 'year': 2020, 'color': 'white', 'Power': '400 HP'}\n"
465
+ ]
466
+ }
467
+ ],
468
+ "source": [
469
+ "dic.pop(\"model\")\n",
470
+ "print(dic)"
471
+ ]
472
+ },
473
+ {
474
+ "cell_type": "code",
475
+ "execution_count": 17,
476
+ "metadata": {
477
+ "colab": {
478
+ "base_uri": "https://localhost:8080/"
479
+ },
480
+ "id": "5rLw_HJGluTk",
481
+ "outputId": "b0a40a0f-034b-4816-a8af-64d3b646bbb2"
482
+ },
483
+ "outputs": [
484
+ {
485
+ "name": "stdout",
486
+ "output_type": "stream",
487
+ "text": [
488
+ "{'brand': 'Ford', 'year': 2020, 'color': 'white'}\n"
489
+ ]
490
+ }
491
+ ],
492
+ "source": [
493
+ "dic.popitem()\n",
494
+ "print(dic)"
495
+ ]
496
+ },
497
+ {
498
+ "cell_type": "code",
499
+ "execution_count": 18,
500
+ "metadata": {
501
+ "colab": {
502
+ "base_uri": "https://localhost:8080/"
503
+ },
504
+ "id": "HXjuyg0Kl2c1",
505
+ "outputId": "ebbd008f-6835-4a42-8426-54817e62e76d"
506
+ },
507
+ "outputs": [
508
+ {
509
+ "name": "stdout",
510
+ "output_type": "stream",
511
+ "text": [
512
+ "{'brand': 'Ford', 'color': 'white'}\n"
513
+ ]
514
+ }
515
+ ],
516
+ "source": [
517
+ "del dic[\"year\"]\n",
518
+ "print(dic)"
519
+ ]
520
+ },
521
+ {
522
+ "cell_type": "code",
523
+ "execution_count": 19,
524
+ "metadata": {
525
+ "colab": {
526
+ "base_uri": "https://localhost:8080/"
527
+ },
528
+ "id": "NaGVNcQLl_Ct",
529
+ "outputId": "8f88b9c5-6606-44c4-86c9-bd0ed5d3cfb4"
530
+ },
531
+ "outputs": [
532
+ {
533
+ "name": "stdout",
534
+ "output_type": "stream",
535
+ "text": [
536
+ "{}\n"
537
+ ]
538
+ }
539
+ ],
540
+ "source": [
541
+ "dic.clear()\n",
542
+ "print(dic)"
543
+ ]
544
+ },
545
+ {
546
+ "cell_type": "markdown",
547
+ "metadata": {
548
+ "id": "2g18Gu0Ck-t9"
549
+ },
550
+ "source": [
551
+ "for loop in dictionary"
552
+ ]
553
+ },
554
+ {
555
+ "cell_type": "code",
556
+ "execution_count": 20,
557
+ "metadata": {
558
+ "id": "sjuLUjk4lMMW"
559
+ },
560
+ "outputs": [],
561
+ "source": [
562
+ "dic = {\n",
563
+ " \"brand\": \"Ford\",\n",
564
+ " \"model\": \"Mustang\",\n",
565
+ " \"year\": 1964\n",
566
+ "}"
567
+ ]
568
+ },
569
+ {
570
+ "cell_type": "markdown",
571
+ "metadata": {
572
+ "id": "SfuHtH52lWkM"
573
+ },
574
+ "source": [
575
+ "for loop with keys"
576
+ ]
577
+ },
578
+ {
579
+ "cell_type": "code",
580
+ "execution_count": 21,
581
+ "metadata": {
582
+ "colab": {
583
+ "base_uri": "https://localhost:8080/"
584
+ },
585
+ "id": "viXaYxIwlOED",
586
+ "outputId": "ca313f60-0338-40df-c3be-c125fca936de"
587
+ },
588
+ "outputs": [
589
+ {
590
+ "name": "stdout",
591
+ "output_type": "stream",
592
+ "text": [
593
+ "brand\n",
594
+ "model\n",
595
+ "year\n"
596
+ ]
597
+ }
598
+ ],
599
+ "source": [
600
+ "for x in dic:\n",
601
+ " print(x)"
602
+ ]
603
+ },
604
+ {
605
+ "cell_type": "code",
606
+ "execution_count": 22,
607
+ "metadata": {
608
+ "colab": {
609
+ "base_uri": "https://localhost:8080/"
610
+ },
611
+ "id": "8tybG7eClb1A",
612
+ "outputId": "4d3545c0-b679-4522-b26d-70364c931348"
613
+ },
614
+ "outputs": [
615
+ {
616
+ "name": "stdout",
617
+ "output_type": "stream",
618
+ "text": [
619
+ "brand\n",
620
+ "model\n",
621
+ "year\n"
622
+ ]
623
+ }
624
+ ],
625
+ "source": [
626
+ "for x in dic.keys():\n",
627
+ " print(x)"
628
+ ]
629
+ },
630
+ {
631
+ "cell_type": "markdown",
632
+ "metadata": {
633
+ "id": "_CT7BsxjlsjZ"
634
+ },
635
+ "source": [
636
+ "for loop with values"
637
+ ]
638
+ },
639
+ {
640
+ "cell_type": "code",
641
+ "execution_count": 23,
642
+ "metadata": {
643
+ "colab": {
644
+ "base_uri": "https://localhost:8080/"
645
+ },
646
+ "id": "hWpbaVpQmFTG",
647
+ "outputId": "f7ab0e1a-2329-41ce-fb4c-747eee4605b7"
648
+ },
649
+ "outputs": [
650
+ {
651
+ "name": "stdout",
652
+ "output_type": "stream",
653
+ "text": [
654
+ "Ford\n",
655
+ "Mustang\n",
656
+ "1964\n"
657
+ ]
658
+ }
659
+ ],
660
+ "source": [
661
+ "for x in dic:\n",
662
+ " print(dic[x])"
663
+ ]
664
+ },
665
+ {
666
+ "cell_type": "code",
667
+ "execution_count": 24,
668
+ "metadata": {
669
+ "colab": {
670
+ "base_uri": "https://localhost:8080/"
671
+ },
672
+ "id": "PFYLUr8GljJA",
673
+ "outputId": "b62871c8-9390-4f84-a783-0d5bfb17de3c"
674
+ },
675
+ "outputs": [
676
+ {
677
+ "name": "stdout",
678
+ "output_type": "stream",
679
+ "text": [
680
+ "Ford\n",
681
+ "Mustang\n",
682
+ "1964\n"
683
+ ]
684
+ }
685
+ ],
686
+ "source": [
687
+ "for x in dic.values():\n",
688
+ " print(x)"
689
+ ]
690
+ },
691
+ {
692
+ "cell_type": "markdown",
693
+ "metadata": {
694
+ "id": "e8ttUHNPl2Ep"
695
+ },
696
+ "source": [
697
+ "for loop with items"
698
+ ]
699
+ },
700
+ {
701
+ "cell_type": "code",
702
+ "execution_count": 25,
703
+ "metadata": {
704
+ "colab": {
705
+ "base_uri": "https://localhost:8080/"
706
+ },
707
+ "id": "7NEARCAimQmX",
708
+ "outputId": "ab8c1e97-025f-4619-bd8b-92731b8fdca3"
709
+ },
710
+ "outputs": [
711
+ {
712
+ "name": "stdout",
713
+ "output_type": "stream",
714
+ "text": [
715
+ "brand : Ford\n",
716
+ "model : Mustang\n",
717
+ "year : 1964\n"
718
+ ]
719
+ }
720
+ ],
721
+ "source": [
722
+ "for x, y in dic.items():\n",
723
+ " print(x,':', y)"
724
+ ]
725
+ },
726
+ {
727
+ "cell_type": "markdown",
728
+ "metadata": {
729
+ "id": "fq8j8EkwnbuQ"
730
+ },
731
+ "source": [
732
+ "Advanced Operations of dictionaries"
733
+ ]
734
+ },
735
+ {
736
+ "cell_type": "code",
737
+ "execution_count": 26,
738
+ "metadata": {
739
+ "id": "GjgwAYbZmtFC"
740
+ },
741
+ "outputs": [],
742
+ "source": [
743
+ "child1 = {\n",
744
+ " \"name\" : \"Rohan\",\n",
745
+ " \"year\" : 2004\n",
746
+ "}\n",
747
+ "child2 = {\n",
748
+ " \"name\" : \"Rakib\",\n",
749
+ " \"year\" : 2007\n",
750
+ "}\n",
751
+ "child3 = {\n",
752
+ " \"name\" : \"Rana\",\n",
753
+ " \"year\" : 2011\n",
754
+ "}\n",
755
+ "\n",
756
+ "myfamily = {\n",
757
+ " \"child1\" : child1,\n",
758
+ " \"child2\" : child2,\n",
759
+ " \"child3\" : child3\n",
760
+ "}"
761
+ ]
762
+ },
763
+ {
764
+ "cell_type": "code",
765
+ "execution_count": 27,
766
+ "metadata": {
767
+ "colab": {
768
+ "base_uri": "https://localhost:8080/"
769
+ },
770
+ "id": "28qYsQ-erbHA",
771
+ "outputId": "330b9c15-a7be-45bc-a02e-970daa992ff2"
772
+ },
773
+ "outputs": [
774
+ {
775
+ "data": {
776
+ "text/plain": [
777
+ "{'child1': {'name': 'Rohan', 'year': 2004},\n",
778
+ " 'child2': {'name': 'Rakib', 'year': 2007},\n",
779
+ " 'child3': {'name': 'Rana', 'year': 2011}}"
780
+ ]
781
+ },
782
+ "execution_count": 27,
783
+ "metadata": {},
784
+ "output_type": "execute_result"
785
+ }
786
+ ],
787
+ "source": [
788
+ "myfamily"
789
+ ]
790
+ },
791
+ {
792
+ "cell_type": "code",
793
+ "execution_count": 28,
794
+ "metadata": {
795
+ "colab": {
796
+ "base_uri": "https://localhost:8080/"
797
+ },
798
+ "id": "k0_Fp2RrnHlY",
799
+ "outputId": "e15c1f6b-25d8-42a9-fa78-409dfe895b25"
800
+ },
801
+ "outputs": [
802
+ {
803
+ "name": "stdout",
804
+ "output_type": "stream",
805
+ "text": [
806
+ "{1: 1, 2: 4, 3: 9, 4: 16, 5: 25}\n"
807
+ ]
808
+ }
809
+ ],
810
+ "source": [
811
+ "dic = {}\n",
812
+ "for x in range(1, 6):\n",
813
+ " dic[x] = x ** 2\n",
814
+ " # dic.update({x: x ** 2})\n",
815
+ "print(dic)"
816
+ ]
817
+ },
818
+ {
819
+ "cell_type": "code",
820
+ "execution_count": 29,
821
+ "metadata": {
822
+ "colab": {
823
+ "base_uri": "https://localhost:8080/"
824
+ },
825
+ "id": "MpLOphmoTesg",
826
+ "outputId": "14fab957-1055-421e-8e2c-b8d18eca45bc"
827
+ },
828
+ "outputs": [
829
+ {
830
+ "name": "stdout",
831
+ "output_type": "stream",
832
+ "text": [
833
+ "{1: 1, 2: 4, 3: 9, 4: 16, 5: 25}\n"
834
+ ]
835
+ }
836
+ ],
837
+ "source": [
838
+ "squares = {x: x ** 2 for x in range(1, 6)}\n",
839
+ "print(squares)"
840
+ ]
841
+ },
842
+ {
843
+ "cell_type": "code",
844
+ "execution_count": 30,
845
+ "metadata": {
846
+ "colab": {
847
+ "base_uri": "https://localhost:8080/"
848
+ },
849
+ "id": "w6EtOS4wookU",
850
+ "outputId": "2a19cdd0-2f67-4b01-cbe6-28c5b1d193d5"
851
+ },
852
+ "outputs": [
853
+ {
854
+ "name": "stdout",
855
+ "output_type": "stream",
856
+ "text": [
857
+ "{2: 4, 4: 16, 6: 36, 8: 64, 10: 100}\n"
858
+ ]
859
+ }
860
+ ],
861
+ "source": [
862
+ "dic = {}\n",
863
+ "for x in range(1, 11):\n",
864
+ " if x % 2 == 0:\n",
865
+ " dic[x] = x ** 2\n",
866
+ " # dic.update({x: x ** 2})\n",
867
+ "print(dic)"
868
+ ]
869
+ },
870
+ {
871
+ "cell_type": "code",
872
+ "execution_count": 31,
873
+ "metadata": {
874
+ "colab": {
875
+ "base_uri": "https://localhost:8080/"
876
+ },
877
+ "id": "dPjzWfdsau9g",
878
+ "outputId": "9460ce1d-0cbc-4449-82b8-53e11183df28"
879
+ },
880
+ "outputs": [
881
+ {
882
+ "name": "stdout",
883
+ "output_type": "stream",
884
+ "text": [
885
+ "{2: 4, 4: 16, 6: 36, 8: 64, 10: 100}\n"
886
+ ]
887
+ }
888
+ ],
889
+ "source": [
890
+ "squares = {x: x ** 2 for x in range(1, 11) if x % 2 == 0}\n",
891
+ "print(squares)"
892
+ ]
893
+ },
894
+ {
895
+ "cell_type": "code",
896
+ "execution_count": 39,
897
+ "metadata": {
898
+ "id": "9OtTs6tC7Zcf"
899
+ },
900
+ "outputs": [],
901
+ "source": [
902
+ "users = [\n",
903
+ " {\n",
904
+ " \"_id\": \"u12345\",\n",
905
+ " \"name\": \"Rahman\",\n",
906
+ " \"email\": \"rahman@example.com\",\n",
907
+ " \"age\": 28,\n",
908
+ " \"isActive\": True,\n",
909
+ " \"address\": {\n",
910
+ " \"street\": \"123 Lakeview Road\",\n",
911
+ " \"city\": \"Dhaka\",\n",
912
+ " \"zip\": \"1207\"\n",
913
+ " },\n",
914
+ " \"hobbies\": [\"reading\", \"gaming\", \"traveling\"],\n",
915
+ " \"orders\": [\n",
916
+ " {\n",
917
+ " \"order_id\": \"ord1001\",\n",
918
+ " \"date\": \"2025-05-01\",\n",
919
+ " \"items\": [\n",
920
+ " {\"product\": \"Book\", \"price\": 12.99, \"quantity\": 1},\n",
921
+ " {\"product\": \"Pen\", \"price\": 1.99, \"quantity\": 3}\n",
922
+ " ],\n",
923
+ " \"total\": 18.96\n",
924
+ " },\n",
925
+ " {\n",
926
+ " \"order_id\": \"ord1002\",\n",
927
+ " \"date\": \"2025-05-04\",\n",
928
+ " \"items\": [\n",
929
+ " {\"product\": \"Shoes\", \"price\": 45.00, \"quantity\": 1}\n",
930
+ " ],\n",
931
+ " \"total\": 45.00\n",
932
+ " }\n",
933
+ " ]\n",
934
+ " },\n",
935
+ " {\n",
936
+ " \"_id\": \"u12346\",\n",
937
+ " \"name\": \"Fatima\",\n",
938
+ " \"email\": \"fatima@example.com\",\n",
939
+ " \"age\": 32,\n",
940
+ " \"isActive\": False,\n",
941
+ " \"address\": {\n",
942
+ " \"street\": \"78 Rose Lane\",\n",
943
+ " \"city\": \"Chittagong\",\n",
944
+ " \"zip\": \"4000\"\n",
945
+ " },\n",
946
+ " \"hobbies\": [\"painting\", \"yoga\"],\n",
947
+ " \"orders\": [\n",
948
+ " {\n",
949
+ " \"order_id\": \"ord1003\",\n",
950
+ " \"date\": \"2025-05-02\",\n",
951
+ " \"items\": [\n",
952
+ " {\"product\": \"Canvas\", \"price\": 25.50, \"quantity\": 2}\n",
953
+ " ],\n",
954
+ " \"total\": 51.00\n",
955
+ " }\n",
956
+ " ]\n",
957
+ " },\n",
958
+ " {\n",
959
+ " \"_id\": \"u12347\",\n",
960
+ " \"name\": \"Karim\",\n",
961
+ " \"email\": \"karim@example.com\",\n",
962
+ " \"age\": 40,\n",
963
+ " \"isActive\": True,\n",
964
+ " \"address\": {\n",
965
+ " \"street\": \"10 Green Street\",\n",
966
+ " \"city\": \"Khulna\",\n",
967
+ " \"zip\": \"9100\"\n",
968
+ " },\n",
969
+ " \"hobbies\": [\"fishing\", \"cycling\", \"chess\"],\n",
970
+ " \"orders\": []\n",
971
+ " },\n",
972
+ " {\n",
973
+ " \"_id\": \"u12348\",\n",
974
+ " \"name\": \"Ayesha\",\n",
975
+ " \"email\": \"ayesha@example.com\",\n",
976
+ " \"age\": 25,\n",
977
+ " \"isActive\": True,\n",
978
+ " \"address\": {\n",
979
+ " \"street\": \"56 Mango Street\",\n",
980
+ " \"city\": \"Sylhet\",\n",
981
+ " \"zip\": \"3100\"\n",
982
+ " },\n",
983
+ " \"hobbies\": [\"dancing\", \"baking\"],\n",
984
+ " \"orders\": [\n",
985
+ " {\n",
986
+ " \"order_id\": \"ord1004\",\n",
987
+ " \"date\": \"2025-05-05\",\n",
988
+ " \"items\": [\n",
989
+ " {\"product\": \"Dress\", \"price\": 30.00, \"quantity\": 1},\n",
990
+ " {\"product\": \"Handbag\", \"price\": 40.00, \"quantity\": 1}\n",
991
+ " ],\n",
992
+ " \"total\": 70.00\n",
993
+ " }\n",
994
+ " ]\n",
995
+ " }\n",
996
+ "]\n"
997
+ ]
998
+ },
999
+ {
1000
+ "cell_type": "code",
1001
+ "execution_count": 46,
1002
+ "metadata": {},
1003
+ "outputs": [
1004
+ {
1005
+ "data": {
1006
+ "text/plain": [
1007
+ "[{'_id': 'u12345',\n",
1008
+ " 'name': 'Rahman',\n",
1009
+ " 'email': 'rahman@example.com',\n",
1010
+ " 'age': 28,\n",
1011
+ " 'isActive': True,\n",
1012
+ " 'address': {'street': '123 Lakeview Road', 'city': 'Dhaka', 'zip': '1207'},\n",
1013
+ " 'hobbies': ['reading', 'gaming', 'traveling'],\n",
1014
+ " 'orders': [{'order_id': 'ord1001',\n",
1015
+ " 'date': '2025-05-01',\n",
1016
+ " 'items': [{'product': 'Book', 'price': 12.99, 'quantity': 1},\n",
1017
+ " {'product': 'Pen', 'price': 1.99, 'quantity': 3}],\n",
1018
+ " 'total': 18.96},\n",
1019
+ " {'order_id': 'ord1002',\n",
1020
+ " 'date': '2025-05-04',\n",
1021
+ " 'items': [{'product': 'Shoes', 'price': 45.0, 'quantity': 1}],\n",
1022
+ " 'total': 45.0}]},\n",
1023
+ " {'_id': 'u12347',\n",
1024
+ " 'name': 'Karim',\n",
1025
+ " 'email': 'karim@example.com',\n",
1026
+ " 'age': 40,\n",
1027
+ " 'isActive': True,\n",
1028
+ " 'address': {'street': '10 Green Street', 'city': 'Khulna', 'zip': '9100'},\n",
1029
+ " 'hobbies': ['fishing', 'cycling', 'chess'],\n",
1030
+ " 'orders': []},\n",
1031
+ " {'_id': 'u12348',\n",
1032
+ " 'name': 'Ayesha',\n",
1033
+ " 'email': 'ayesha@example.com',\n",
1034
+ " 'age': 25,\n",
1035
+ " 'isActive': True,\n",
1036
+ " 'address': {'street': '56 Mango Street', 'city': 'Sylhet', 'zip': '3100'},\n",
1037
+ " 'hobbies': ['dancing', 'baking'],\n",
1038
+ " 'orders': [{'order_id': 'ord1004',\n",
1039
+ " 'date': '2025-05-05',\n",
1040
+ " 'items': [{'product': 'Dress', 'price': 30.0, 'quantity': 1},\n",
1041
+ " {'product': 'Handbag', 'price': 40.0, 'quantity': 1}],\n",
1042
+ " 'total': 70.0}]}]"
1043
+ ]
1044
+ },
1045
+ "execution_count": 46,
1046
+ "metadata": {},
1047
+ "output_type": "execute_result"
1048
+ }
1049
+ ],
1050
+ "source": [
1051
+ "active_users = [user for user in users if user[\"isActive\"]]\n",
1052
+ "active_users"
1053
+ ]
1054
+ },
1055
+ {
1056
+ "cell_type": "code",
1057
+ "execution_count": 47,
1058
+ "metadata": {},
1059
+ "outputs": [
1060
+ {
1061
+ "data": {
1062
+ "text/plain": [
1063
+ "[{'name': 'Rahman', 'email': 'rahman@example.com'},\n",
1064
+ " {'name': 'Fatima', 'email': 'fatima@example.com'},\n",
1065
+ " {'name': 'Ayesha', 'email': 'ayesha@example.com'}]"
1066
+ ]
1067
+ },
1068
+ "execution_count": 47,
1069
+ "metadata": {},
1070
+ "output_type": "execute_result"
1071
+ }
1072
+ ],
1073
+ "source": [
1074
+ "users_with_orders = [\n",
1075
+ " {\"name\": user[\"name\"], \"email\": user[\"email\"]}\n",
1076
+ " for user in users if len(user[\"orders\"]) > 0\n",
1077
+ "]\n",
1078
+ "users_with_orders"
1079
+ ]
1080
+ },
1081
+ {
1082
+ "cell_type": "code",
1083
+ "execution_count": 48,
1084
+ "metadata": {},
1085
+ "outputs": [
1086
+ {
1087
+ "data": {
1088
+ "text/plain": [
1089
+ "[{'name': 'Rahman', 'total_spent': 63.96},\n",
1090
+ " {'name': 'Fatima', 'total_spent': 51.0},\n",
1091
+ " {'name': 'Karim', 'total_spent': 0},\n",
1092
+ " {'name': 'Ayesha', 'total_spent': 70.0}]"
1093
+ ]
1094
+ },
1095
+ "execution_count": 48,
1096
+ "metadata": {},
1097
+ "output_type": "execute_result"
1098
+ }
1099
+ ],
1100
+ "source": [
1101
+ "user_spending = [\n",
1102
+ " {\n",
1103
+ " \"name\": user[\"name\"],\n",
1104
+ " \"total_spent\": sum(order[\"total\"] for order in user[\"orders\"])\n",
1105
+ " }\n",
1106
+ " for user in users\n",
1107
+ "]\n",
1108
+ "user_spending"
1109
+ ]
1110
+ },
1111
+ {
1112
+ "cell_type": "code",
1113
+ "execution_count": 53,
1114
+ "metadata": {},
1115
+ "outputs": [
1116
+ {
1117
+ "name": "stdout",
1118
+ "output_type": "stream",
1119
+ "text": [
1120
+ "Book\n",
1121
+ "Pen\n",
1122
+ "Shoes\n"
1123
+ ]
1124
+ }
1125
+ ],
1126
+ "source": [
1127
+ "for user in users:\n",
1128
+ " if user['name'] == 'Rahman':\n",
1129
+ " for i in user['orders']:\n",
1130
+ " for j in i['items']:\n",
1131
+ " print(j['product'])"
1132
+ ]
1133
+ },
1134
+ {
1135
+ "cell_type": "code",
1136
+ "execution_count": 55,
1137
+ "metadata": {},
1138
+ "outputs": [
1139
+ {
1140
+ "data": {
1141
+ "text/plain": [
1142
+ "31.25"
1143
+ ]
1144
+ },
1145
+ "execution_count": 55,
1146
+ "metadata": {},
1147
+ "output_type": "execute_result"
1148
+ }
1149
+ ],
1150
+ "source": [
1151
+ "avg_age = sum(user[\"age\"] for user in users) / len(users)\n",
1152
+ "avg_age"
1153
+ ]
1154
+ },
1155
+ {
1156
+ "cell_type": "code",
1157
+ "execution_count": 63,
1158
+ "metadata": {},
1159
+ "outputs": [
1160
+ {
1161
+ "ename": "SyntaxError",
1162
+ "evalue": "invalid syntax (3403861915.py, line 5)",
1163
+ "output_type": "error",
1164
+ "traceback": [
1165
+ " \u001b[36mCell\u001b[39m\u001b[36m \u001b[39m\u001b[32mIn[63]\u001b[39m\u001b[32m, line 5\u001b[39m\n\u001b[31m \u001b[39m\u001b[31mproduct_summary.update({[item[\"product\"]] += item[\"quantity\"]})\u001b[39m\n ^\n\u001b[31mSyntaxError\u001b[39m\u001b[31m:\u001b[39m invalid syntax\n"
1166
+ ]
1167
+ }
1168
+ ],
1169
+ "source": [
1170
+ "product_summary = {}\n",
1171
+ "for user in users:\n",
1172
+ " for order in user[\"orders\"]:\n",
1173
+ " for item in order[\"items\"]:\n",
1174
+ " product_summary.update({[item[\"product\"]] += item[\"quantity\"]})\n",
1175
+ "product_summary"
1176
+ ]
1177
+ },
1178
+ {
1179
+ "cell_type": "code",
1180
+ "execution_count": 65,
1181
+ "metadata": {},
1182
+ "outputs": [
1183
+ {
1184
+ "data": {
1185
+ "text/plain": [
1186
+ "1"
1187
+ ]
1188
+ },
1189
+ "execution_count": 65,
1190
+ "metadata": {},
1191
+ "output_type": "execute_result"
1192
+ }
1193
+ ],
1194
+ "source": [
1195
+ "dhaka_count = sum(1 for user in users if user[\"address\"][\"city\"] == \"Dhaka\")\n",
1196
+ "dhaka_count"
1197
+ ]
1198
+ }
1199
+ ],
1200
+ "metadata": {
1201
+ "colab": {
1202
+ "provenance": []
1203
+ },
1204
+ "kernelspec": {
1205
+ "display_name": "all",
1206
+ "language": "python",
1207
+ "name": "python3"
1208
+ },
1209
+ "language_info": {
1210
+ "codemirror_mode": {
1211
+ "name": "ipython",
1212
+ "version": 3
1213
+ },
1214
+ "file_extension": ".py",
1215
+ "mimetype": "text/x-python",
1216
+ "name": "python",
1217
+ "nbconvert_exporter": "python",
1218
+ "pygments_lexer": "ipython3",
1219
+ "version": "3.12.9"
1220
+ }
1221
+ },
1222
+ "nbformat": 4,
1223
+ "nbformat_minor": 0
1224
+ }