Dict Comprehensions¶
Let us see how we can process Python collections using dict comprehensions.
- We will be using orders data set which will be available by running notebook related to preparing data sets.
- We will see how to get a dict using dict comprehensions. The output should contain key value pairs for each order where order_id is key and order_customer_id is the value.
In [1]:
%run 07_preparing_data_sets.ipynb
In [2]:
orders[:10]
Out[2]:
['1,2013-07-25 00:00:00.0,11599,CLOSED', '2,2013-07-25 00:00:00.0,256,PENDING_PAYMENT', '3,2013-07-25 00:00:00.0,12111,COMPLETE', '4,2013-07-25 00:00:00.0,8827,CLOSED', '5,2013-07-25 00:00:00.0,11318,COMPLETE', '6,2013-07-25 00:00:00.0,7130,COMPLETE', '7,2013-07-25 00:00:00.0,4530,COMPLETE', '8,2013-07-25 00:00:00.0,2911,PROCESSING', '9,2013-07-25 00:00:00.0,5657,PENDING_PAYMENT', '10,2013-07-25 00:00:00.0,5648,PENDING_PAYMENT']
In [3]:
len(orders)
Out[3]:
68883
In [4]:
order_customers = {int(order.split(',')[0]): int(order.split(',')[2]) for order in orders}
In [5]:
type(order_customers)
Out[5]:
dict
In [6]:
order_customers
Out[6]:
{1: 11599, 2: 256, 3: 12111, 4: 8827, 5: 11318, 6: 7130, 7: 4530, 8: 2911, 9: 5657, 10: 5648, 11: 918, 12: 1837, 13: 9149, 14: 9842, 15: 2568, 16: 7276, 17: 2667, 18: 1205, 19: 9488, 20: 9198, 21: 2711, 22: 333, 23: 4367, 24: 11441, 25: 9503, 26: 7562, 27: 3241, 28: 656, 29: 196, 30: 10039, 31: 6983, 32: 3960, 33: 5793, 34: 4189, 35: 4840, 36: 5649, 37: 5863, 38: 11586, 39: 8214, 40: 12092, 41: 8136, 42: 9776, 43: 7776, 44: 10500, 45: 2636, 46: 1549, 47: 8487, 48: 12186, 49: 1871, 50: 5225, 51: 12271, 52: 5126, 53: 4701, 54: 10628, 55: 2052, 56: 10519, 57: 7073, 58: 9213, 59: 11644, 60: 8365, 61: 4791, 62: 9111, 63: 1148, 64: 5579, 65: 5903, 66: 1792, 67: 1406, 68: 4320, 69: 2821, 70: 11809, 71: 8646, 72: 4349, 73: 8504, 74: 662, 75: 2505, 76: 6898, 77: 7915, 78: 8619, 79: 7327, 80: 3007, 81: 674, 82: 3566, 83: 1265, 84: 6789, 85: 1485, 86: 6680, 87: 3065, 88: 3809, 89: 824, 90: 9131, 91: 8912, 92: 6932, 93: 2256, 94: 11589, 95: 9032, 96: 8683, 97: 10784, 98: 5243, 99: 11542, 100: 12131, 101: 5116, 102: 8027, 103: 12256, 104: 7790, 105: 8220, 106: 395, 107: 1845, 108: 12149, 109: 9345, 110: 2746, 111: 4271, 112: 5375, 113: 10216, 114: 2091, 115: 104, 116: 8763, 117: 58, 118: 1737, 119: 8124, 120: 356, 121: 2074, 122: 2071, 123: 3695, 124: 2374, 125: 4611, 126: 610, 127: 5261, 128: 2772, 129: 9937, 130: 7509, 131: 10072, 132: 289, 133: 10604, 134: 12081, 135: 7738, 136: 9126, 137: 4211, 138: 10904, 139: 7933, 140: 4257, 141: 12128, 142: 5760, 143: 10495, 144: 2158, 145: 494, 146: 8177, 147: 275, 148: 5383, 149: 11431, 150: 236, 151: 2493, 152: 3611, 153: 8876, 154: 9993, 155: 9044, 156: 5200, 157: 8986, 158: 12345, 159: 10974, 160: 6762, 161: 4513, 162: 3304, 163: 3348, 164: 2962, 165: 1222, 166: 7834, 167: 1347, 168: 11794, 169: 2611, 170: 6295, 171: 1980, 172: 11382, 173: 9027, 174: 3358, 175: 384, 176: 2897, 177: 9151, 178: 8503, 179: 6506, 180: 10215, 181: 7473, 182: 10984, 183: 12105, 184: 210, 185: 10966, 186: 1104, 187: 1378, 188: 2889, 189: 10177, 190: 11115, 191: 16, 192: 5293, 193: 6174, 194: 2550, 195: 12096, 196: 2765, 197: 6370, 198: 7436, 199: 4608, 200: 11116, 201: 9055, 202: 8524, 203: 11791, 204: 7604, 205: 3109, 206: 8994, 207: 11643, 208: 7923, 209: 3627, 210: 10102, 211: 10372, 212: 3190, 213: 11715, 214: 3925, 215: 5925, 216: 7282, 217: 5420, 218: 3935, 219: 10655, 220: 1383, 221: 11871, 222: 8309, 223: 9068, 224: 11938, 225: 11275, 226: 9356, 227: 7410, 228: 6070, 229: 618, 230: 4921, 231: 3960, 232: 1780, 233: 12316, 234: 1010, 235: 4285, 236: 9785, 237: 11438, 238: 4939, 239: 5058, 240: 32, 241: 948, 242: 3237, 243: 32, 244: 6910, 245: 5191, 246: 9616, 247: 173, 248: 11707, 249: 7650, 250: 11668, 251: 568, 252: 9708, 253: 45, 254: 9395, 255: 10346, 256: 4199, 257: 3273, 258: 1137, 259: 3454, 260: 5135, 261: 5128, 262: 11475, 263: 8026, 264: 230, 265: 488, 266: 11085, 267: 6989, 268: 5578, 269: 11887, 270: 5689, 271: 815, 272: 428, 273: 10665, 274: 3365, 275: 10942, 276: 2221, 277: 3732, 278: 488, 279: 10372, 280: 11836, 281: 827, 282: 3551, 283: 11466, 284: 861, 285: 5011, 286: 9824, 287: 8122, 288: 8667, 289: 1195, 290: 6446, 291: 5506, 292: 11971, 293: 4731, 294: 11394, 295: 1937, 296: 6665, 297: 2281, 298: 4967, 299: 9202, 300: 11326, 301: 11999, 302: 3577, 303: 1682, 304: 1869, 305: 5618, 306: 137, 307: 8818, 308: 6829, 309: 10396, 310: 1256, 311: 410, 312: 1122, 313: 7543, 314: 10033, 315: 4375, 316: 3870, 317: 1450, 318: 8829, 319: 10445, 320: 10698, 321: 4282, 322: 2864, 323: 3020, 324: 76, 325: 4759, 326: 1604, 327: 12107, 328: 1149, 329: 1944, 330: 5175, 331: 4797, 332: 4171, 333: 2071, 334: 3752, 335: 7710, 336: 5479, 337: 6747, 338: 4666, 339: 8326, 340: 7995, 341: 10128, 342: 8809, 343: 10952, 344: 2816, 345: 11040, 346: 3646, 347: 11662, 348: 12120, 349: 9103, 350: 10672, 351: 12049, 352: 1977, 353: 2234, 354: 5862, 355: 11682, 356: 2209, 357: 2685, 358: 2979, 359: 6020, 360: 8020, 361: 8286, 362: 10167, 363: 3619, 364: 6035, 365: 2400, 366: 4422, 367: 8730, 368: 5783, 369: 189, 370: 4791, 371: 4428, 372: 776, 373: 3630, 374: 5906, 375: 6561, 376: 1369, 377: 514, 378: 10071, 379: 8735, 380: 9505, 381: 5756, 382: 8143, 383: 10509, 384: 2610, 385: 10667, 386: 3568, 387: 7193, 388: 2790, 389: 4435, 390: 5590, 391: 5816, 392: 3238, 393: 1047, 394: 76, 395: 3934, 396: 9078, 397: 4476, 398: 1365, 399: 1473, 400: 5626, 401: 9692, 402: 9547, 403: 4178, 404: 1478, 405: 9498, 406: 9023, 407: 3333, 408: 2252, 409: 11102, 410: 1288, 411: 11164, 412: 9397, 413: 7278, 414: 11776, 415: 6110, 416: 1561, 417: 11491, 418: 4269, 419: 8987, 420: 4632, 421: 7839, 422: 9664, 423: 9632, 424: 2457, 425: 10481, 426: 10419, 427: 6681, 428: 3487, 429: 8466, 430: 10779, 431: 4676, 432: 1197, 433: 792, 434: 11390, 435: 5719, 436: 7610, 437: 5351, 438: 8256, 439: 9353, 440: 7290, 441: 5239, 442: 8098, 443: 8499, 444: 10004, 445: 3859, 446: 7620, 447: 8737, 448: 7167, 449: 409, 450: 5085, 451: 10104, 452: 5128, 453: 381, 454: 5933, 455: 6575, 456: 11295, 457: 4036, 458: 5358, 459: 3687, 460: 2521, 461: 8398, 462: 5454, 463: 8709, 464: 7114, 465: 1255, 466: 10943, 467: 1719, 468: 7305, 469: 1731, 470: 6410, 471: 10861, 472: 8969, 473: 5359, 474: 8349, 475: 4567, 476: 6020, 477: 8511, 478: 7657, 479: 11138, 480: 2902, 481: 10633, 482: 9156, 483: 12341, 484: 2409, 485: 3976, 486: 1865, 487: 5698, 488: 10143, 489: 2030, 490: 9709, 491: 3945, 492: 2499, 493: 1180, 494: 8121, 495: 7242, 496: 615, 497: 846, 498: 6828, 499: 8920, 500: 2129, 501: 1987, 502: 7196, 503: 6377, 504: 6288, 505: 11952, 506: 3509, 507: 4730, 508: 6970, 509: 10739, 510: 7533, 511: 486, 512: 12000, 513: 10479, 514: 10962, 515: 5921, 516: 9204, 517: 1578, 518: 5257, 519: 4780, 520: 8074, 521: 6497, 522: 12300, 523: 4477, 524: 7622, 525: 10830, 526: 7259, 527: 5426, 528: 6147, 529: 844, 530: 6224, 531: 1950, 532: 6498, 533: 6770, 534: 6896, 535: 66, 536: 9981, 537: 10437, 538: 4871, 539: 7673, 540: 121, 541: 4985, 542: 597, 543: 7351, 544: 281, 545: 11642, 546: 7604, 547: 7220, 548: 6889, 549: 3834, 550: 9666, 551: 2867, 552: 1445, 553: 7307, 554: 10980, 555: 976, 556: 7838, 557: 4855, 558: 4562, 559: 1944, 560: 744, 561: 6059, 562: 5017, 563: 3514, 564: 2216, 565: 3850, 566: 6335, 567: 9527, 568: 11790, 569: 8532, 570: 4622, 571: 5006, 572: 6691, 573: 5964, 574: 7145, 575: 2873, 576: 7191, 577: 457, 578: 2529, 579: 4137, 580: 8677, 581: 3222, 582: 6220, 583: 4695, 584: 1263, 585: 8399, 586: 10225, 587: 6008, 588: 6348, 589: 5609, 590: 132, 591: 6431, 592: 10862, 593: 6667, 594: 1946, 595: 3531, 596: 8352, 597: 11558, 598: 11344, 599: 8005, 600: 3506, 601: 8977, 602: 6690, 603: 6184, 604: 6930, 605: 2879, 606: 1157, 607: 6376, 608: 10388, 609: 1241, 610: 1866, 611: 8422, 612: 11854, 613: 460, 614: 5738, 615: 2827, 616: 11330, 617: 6154, 618: 12037, 619: 11473, 620: 11346, 621: 7916, 622: 3892, 623: 3658, 624: 1821, 625: 5918, 626: 9123, 627: 7859, 628: 9783, 629: 6772, 630: 3449, 631: 9381, 632: 529, 633: 12249, 634: 4588, 635: 1427, 636: 10371, 637: 2259, 638: 3626, 639: 1072, 640: 6199, 641: 11558, 642: 7499, 643: 64, 644: 7295, 645: 2238, 646: 911, 647: 12319, 648: 4551, 649: 7261, 650: 4170, 651: 9264, 652: 8732, 653: 10473, 654: 286, 655: 1206, 656: 1646, 657: 1393, 658: 12386, 659: 6006, 660: 1932, 661: 2991, 662: 11165, 663: 3531, 664: 4469, 665: 5648, 666: 993, 667: 4726, 668: 8023, 669: 909, 670: 482, 671: 7926, 672: 1670, 673: 2157, 674: 1295, 675: 3414, 676: 5653, 677: 5182, 678: 9667, 679: 5535, 680: 4599, 681: 7524, 682: 9271, 683: 7183, 684: 1546, 685: 10745, 686: 7835, 687: 11639, 688: 5097, 689: 8272, 690: 7279, 691: 517, 692: 442, 693: 10081, 694: 5015, 695: 12224, 696: 11166, 697: 3126, 698: 5283, 699: 7472, 700: 2632, 701: 9742, 702: 1826, 703: 1871, 704: 11708, 705: 9639, 706: 10129, 707: 5889, 708: 5695, 709: 3161, 710: 5871, 711: 4688, 712: 4372, 713: 4826, 714: 4756, 715: 864, 716: 2581, 717: 8208, 718: 70, 719: 4768, 720: 3142, 721: 2154, 722: 9781, 723: 371, 724: 797, 725: 3837, 726: 5407, 727: 1049, 728: 5965, 729: 8467, 730: 3189, 731: 5104, 732: 7633, 733: 2724, 734: 1995, 735: 9142, 736: 8536, 737: 3567, 738: 10042, 739: 5647, 740: 6271, 741: 7481, 742: 9860, 743: 2030, 744: 2960, 745: 7435, 746: 9753, 747: 4788, 748: 1198, 749: 9270, 750: 4293, 751: 1438, 752: 3366, 753: 5094, 754: 6699, 755: 1478, 756: 8441, 757: 1648, 758: 1244, 759: 3328, 760: 444, 761: 9748, 762: 1748, 763: 943, 764: 8625, 765: 2425, 766: 12328, 767: 10174, 768: 8123, 769: 11749, 770: 12146, 771: 9026, 772: 2863, 773: 10414, 774: 11421, 775: 3547, 776: 3180, 777: 7761, 778: 5759, 779: 2004, 780: 2016, 781: 7971, 782: 12213, 783: 8321, 784: 3321, 785: 8191, 786: 8974, 787: 1150, 788: 5625, 789: 17, 790: 103, 791: 9961, 792: 12218, 793: 11007, 794: 7683, 795: 7219, 796: 11292, 797: 6527, 798: 8709, 799: 4675, 800: 9946, 801: 532, 802: 1670, 803: 5569, 804: 2697, 805: 8259, 806: 11113, 807: 1231, 808: 10509, 809: 7725, 810: 2484, 811: 10491, 812: 7073, 813: 2027, 814: 11988, 815: 11713, 816: 794, 817: 9975, 818: 8459, 819: 1714, 820: 768, 821: 8384, 822: 1492, 823: 9469, 824: 277, 825: 7429, 826: 3744, 827: 11167, 828: 2606, 829: 8977, 830: 9103, 831: 12064, 832: 132, 833: 4076, 834: 481, 835: 10640, 836: 2372, 837: 7715, 838: 3469, 839: 377, 840: 5123, 841: 8410, 842: 4038, 843: 8345, 844: 6061, 845: 5715, 846: 5643, 847: 12163, 848: 6018, 849: 8663, 850: 7382, 851: 11798, 852: 11564, 853: 5542, 854: 4186, 855: 4535, 856: 9876, 857: 833, 858: 9137, 859: 3432, 860: 9725, 861: 3164, 862: 1299, 863: 6081, 864: 12342, 865: 7052, 866: 6788, 867: 6669, 868: 2957, 869: 8657, 870: 6516, 871: 6923, 872: 6440, 873: 2117, 874: 6230, 875: 8330, 876: 7325, 877: 5012, 878: 207, 879: 3031, 880: 5400, 881: 3291, 882: 4102, 883: 4305, 884: 4909, 885: 7706, 886: 2124, 887: 4254, 888: 6610, 889: 3677, 890: 8908, 891: 5609, 892: 9537, 893: 3106, 894: 2759, 895: 7575, 896: 478, 897: 9070, 898: 5271, 899: 7707, 900: 8585, 901: 10184, 902: 1641, 903: 2199, 904: 5854, 905: 5460, 906: 7141, 907: 10661, 908: 8964, 909: 8670, 910: 2762, 911: 3608, 912: 3076, 913: 6408, 914: 6484, 915: 8495, 916: 48, 917: 9901, 918: 780, 919: 10890, 920: 12019, 921: 12, 922: 3160, 923: 5511, 924: 6615, 925: 937, 926: 8052, 927: 2520, 928: 2487, 929: 8482, 930: 954, 931: 10514, 932: 7231, 933: 446, 934: 2931, 935: 10086, 936: 7202, 937: 6899, 938: 7802, 939: 2964, 940: 5098, 941: 842, 942: 9704, 943: 2346, 944: 2917, 945: 9732, 946: 10961, 947: 2637, 948: 4289, 949: 2674, 950: 4151, 951: 3129, 952: 2250, 953: 368, 954: 5066, 955: 8117, 956: 4484, 957: 9879, 958: 12050, 959: 8993, 960: 1463, 961: 5079, 962: 9492, 963: 10558, 964: 3397, 965: 1907, 966: 11298, 967: 1706, 968: 7845, 969: 9247, 970: 7623, 971: 9478, 972: 10255, 973: 5118, 974: 6413, 975: 638, 976: 1088, 977: 7817, 978: 4130, 979: 11323, 980: 7473, 981: 5411, 982: 2873, 983: 9514, 984: 12350, 985: 5389, 986: 7820, 987: 7418, 988: 2491, 989: 370, 990: 7254, 991: 7317, 992: 7107, 993: 5122, 994: 4979, 995: 10907, 996: 12221, 997: 8786, 998: 1739, 999: 9358, 1000: 2321, ...}
In [7]:
order_customers[1]
Out[7]:
11599
In [8]:
employees = [
'1,ktrett0@independent.co.uk,6998.95',
'2,khaddock1@deviantart.com,10572.4',
'3,ecraft2@dell.com,3967.35',
'4,drussam3@t-online.de,17672.44',
'5,graigatt4@github.io,11660.67',
'6,bjaxon5@salon.com,18614.93',
'7,araulston6@list-manage.com,11550.75',
'8,mcobb7@mozilla.com,17016.15',
'9,grobardley8@unesco.org,14141.25',
'10,bbuye9@vkontakte.ru,12193.2'
]
In [9]:
highest_salaried_employees = {
int(employee.split(',')[0]): float(employee.split(',')[2])
for employee in employees
if float(employee.split(',')[2]) > 10000
}
In [10]:
highest_salaried_employees
Out[10]:
{2: 10572.4, 4: 17672.44, 5: 11660.67, 6: 18614.93, 7: 11550.75, 8: 17016.15, 9: 14141.25, 10: 12193.2}