[{"data":1,"prerenderedAt":-1},["ShallowReactive",2],{"doc-detail-155910-en":3,"doc-seo-155910-105":31,"detail-sidebar-cat-0-en-105":96},{"code":4,"msg":5,"data":6},0,"success",{"doc_id":7,"user_id":8,"nickname":9,"user_avatar":10,"doc_module":4,"category_id":11,"category_name":12,"doc_title":13,"doc_description":14,"doc_content":15,"file_id":16,"file_url":17,"file_type":18,"file_size":19,"view_count":20,"is_deleted":4,"is_public":21,"is_downloadable":21,"audit_status":21,"page_count":22,"language":23,"language_code":24,"site_id":25,"html_lang":24,"table_of_contents":26,"faqs":27,"seo_title":28,"seo_description":14,"update_tm":29,"read_time":30},155910,8796095461564,"Liam","https://ap-avatar.wpscdn.com/davatar_155a257f0dc6eb9ab79c44ca47cae57d",4,"Exam","Lambdas and Dictionaries - 4 - Study Notes","A concise study note on Python lambda expressions and dictionaries, emphasizing how lambdas work as one-line functions and how they differ from functions defined with def. The material covers evaluating lambdas with variables, higher-order function usage, and constructing environment diagrams for multiple example programs. It then introduces dictionaries as unordered key-value mappings, explains mutability and key constraints, and provides syntax patterns for accessing, updating, iterating, and removing entries, including a group_by coding exercise.","LAMBDAS AND DICTIONARIES 4  \n\n| DATA C88C\u003Cbr>Februrary 12, 2024 |\n| --- |\n| 1 Lambdas |\n\nLambda expressions are one-line functions that specify two things: the parameters and the return expression.  \nA lambda expression that takes in no arguments and returns 8:  \nlambda: |8{z} return value  \nA lambda expression that takes two arguments and returns their product:  \nlambda  , y : x  * y  \n{z } | {z }  \nparameters return expression  \nUnlike functions created by a def statement, the function object that a lambda expression creates has no intrinsic name and is not bound to any variable. In fact, nothing changes in the current environment when we evaluate a lambda expression unless we do something with this expression, such as assign it to a variable or pass it as an argument to a higher order function.  \n1. What would Python print?>>> a = lambda: 5 >>> a()  \n>>> a(5)  \n>>> b = lambda: lambda x: 3 >>> b()(15)  \n>>> c = lambda x, y: x + y >>> c(4, 5)  \n>>> d = lambda x: lambda y: x * y >>> d(3)  \n>>> d(3)(3)  \n>>> e = d(2)  \n>>> e(5)  \n>>> f = lambda: print (1)  \n>>> g = f()>>> g  \n 2 Environment Diagrams  \n1. Draw the environment diagram for evaluating the following code def mystery_a(lst):  \ndef mystery_b(color, count):  \nlst .extend([color] * count)  \nreturn mystery_b  \ncolors = [\"purple\", \"pink\", \"brown\"]  \nf = mystery_a(colors)  \nf(\"red\", 3)  \nf(\"blue\", 1)  \n2. If on line 2 and line 4, we replace mystery b with mystery   a, what will change in the environment diagram, if anything?  \n3. If on line 3, we change lst .extend([color] * count) to lst .append([color]* count), what will change, if anything?  \n4. Draw the environment diagram for evaluating the following code def ross(geller, num):  \nreturn geller(monica(num))  \ndef monica(num):  \nif num >= 2:  \nreturn tup[0]  \nreturn tup[num]  \nf = lambda x: x[-1] == \"a\"tup = (\"hola\", \"there\")  \nrachel = ross(f, 5)  \n5. Draw the environment diagram for evaluating the following code def anna(olaf):  \nreturn lambda a, b: olaf or [a] * b  \nhans = [1]  \nelsa = anna(hans .append(4))  \nkristoff = elsa(3, 4)  \n3 Dictionaries  \nDictionaries are data structures which map keys to values. Dictionaries in Python are unordered, unlike real-world dictionaries—in other words, key-value pairs are not arranged in the dictionary in any particular order. Let’s look at an example:  \n>>> pokemon = {'pikachu': 25, 'dragonair': 148, 'mew': 151}>>> pokemon['pikachu']  \n25  \n>>> pokemon['jolteon'] = 135 >>> pokemon  \n{'jolteon': 135, 'pikachu': 25, 'dragonair': 148, 'mew': 151}>>> pokemon['ditto'] = 25  \n>>> pokemon  \n{'jolteon': 135, 'pikachu': 25, 'dragonair': 148,'ditto': 25, 'mew': 151}  \n>>> pokemon['mew'] = 15  \n>>> pokemon  \n{'jolteon': 135, 'pikachu': 25, 'dragonair': 148,'ditto': 25, 'mew': 15}  \nThe keys of a dictionary can be any immutable value, such as numbers, strings, and tuples.1 Dictionaries themselves are mutable; we can add, remove, and change entries after creation. There is only one value per key, however—if we assign a new value to the same key, it overrides any previous value which might have existed.  \nTo access the value of dictionary at key, use the syntax dictionary[key] .  \nElement selection and reassignment work similarly to sequences, except the square brackets contain the key, not an index.  \n• To add val corresponding to key or to replace the current value of key with val: dictionary[key] = val  \n• To iterate over a dictionary’s keys:  \nfor key in dictionary: \\#OR for key in dictionary.keys()  \ndo_stuff()  \n• To iterate over a dictionary’s values:  \nfor value in dictionary .values():  \ndo_stuff()  \n1To be exact, keys must be hashable, which is out of scope for this course. This means that some mutable objects, such as classes, can be used as dictionary keys.  \n• To iterate over a dictionary’s keys and values:  \nfor key, value in dictionary .items():  \ndo_stuff()  \n• To remove an entry in a dictionary:  \ndel dictionary[key]  \n• To get the value corresponding to key and remove the e","cbCaijELj86PcMBm","https://ap.wps.com/l/cbCaijELj86PcMBm","pdf",111077,2,1,8,"English","en",105,"# 1 Lambdas\n## Python lambda basics and evaluation examples\n## Environment diagrams for lambda/def interactions\n# 3 Dictionaries\n## Dictionary properties and key-value syntax\n## Iteration, mutation, and removal\n## 3.1 Questions and group_by function","[{\"question\":\"How do lambda expressions differ from functions created with def?\",\"answer\":\"A lambda expression creates a function object without an intrinsic name and it is not automatically bound to a variable. Nothing in the current environment changes unless the lambda expression is used, such as assigning it to a variable or passing it to a higher-order function.\"},{\"question\":\"What is an environment diagram used for in these examples?\",\"answer\":\"It helps track how names and values are bound during evaluation when running given code snippets, including cases where lambdas replace functions and where list operations change the resulting data.\"},{\"question\":\"How do dictionary operations like iteration and deletion work in Python?\",\"answer\":\"Values can be accessed with dictionary[key], iterated over using keys, values, or items, and entries can be removed with del dictionary[key] or dictionary.pop(key). Dictionaries are mutable, and assigning a new value to an existing key overrides the previous value.\"}]","Lambdas and Dictionaries - 4 - Study Notes | PDF",1787941041,20,{"code":4,"msg":32,"data":33},"ok",{"site_id":25,"language":24,"slug":34,"title":13,"keywords":35,"description":14,"schema_data":36,"social_meta":91,"head_meta":93,"extra_data":95,"updated_unix":29},"lambdas-and-dictionaries-4-study-notes","",{"@graph":37,"@context":90},[38,53,73],{"@type":39,"itemListElement":40},"BreadcrumbList",[41,45,48,51],{"item":42,"name":43,"@type":44,"position":21},"https://docshare.wps.com","Home","ListItem",{"item":46,"name":47,"@type":44,"position":20},"https://docshare.wps.com/document/","Document",{"item":49,"name":12,"@type":44,"position":50},"https://docshare.wps.com/document/exam/",3,{"item":52,"name":13,"@type":44,"position":11},"https://docshare.wps.com/document/lambdas-and-dictionaries-4-study-notes/155910/",{"url":52,"name":13,"@type":54,"image":55,"author":60,"headline":13,"publisher":62,"fileFormat":65,"inLanguage":24,"description":14,"dateModified":66,"datePublished":67,"encodingFormat":65,"isAccessibleForFree":68,"interactionStatistic":69},"DigitalDocument",{"url":56,"@type":57,"width":58,"height":59},"https://docshare.wps.com/thumbnails/lambdas-and-dictionaries-4-study-notes/155910.png","ImageObject",300,407,{"name":9,"@type":61},"Person",{"url":42,"name":63,"@type":64},"DocShare","Organization","application/pdf","2026-09-11","2026-08-28",true,{"@type":70,"interactionType":71,"userInteractionCount":20},"InteractionCounter",{"@type":72},"ViewAction",{"@type":74,"mainEntity":75},"FAQPage",[76,82,86],{"name":77,"@type":78,"acceptedAnswer":79},"How do lambda expressions differ from functions created with def?","Question",{"text":80,"@type":81},"A lambda expression creates a function object without an intrinsic name and it is not automatically bound to a variable. Nothing in the current environment changes unless the lambda expression is used, such as assigning it to a variable or passing it to a higher-order function.","Answer",{"name":83,"@type":78,"acceptedAnswer":84},"What is an environment diagram used for in these examples?",{"text":85,"@type":81},"It helps track how names and values are bound during evaluation when running given code snippets, including cases where lambdas replace functions and where list operations change the resulting data.",{"name":87,"@type":78,"acceptedAnswer":88},"How do dictionary operations like iteration and deletion work in Python?",{"text":89,"@type":81},"Values can be accessed with dictionary[key], iterated over using keys, values, or items, and entries can be removed with del dictionary[key] or dictionary.pop(key). Dictionaries are mutable, and assigning a new value to an existing key overrides the previous value.","https://schema.org",{"og:url":52,"og:type":92,"og:title":13,"og:site_name":63,"og:description":14},"article",{"robots":94,"canonical":52},"index,follow",{"doc_id":7,"site_id":25},{"code":4,"msg":5,"data":97},[98,102,106,109,114,119,124,128,132,135,139],{"id":21,"doc_module":4,"doc_module_name":47,"category_name":99,"show_sort_weight":100,"slug":101},"Story & Novel",90,"story-novel",{"id":20,"doc_module":4,"doc_module_name":47,"category_name":103,"show_sort_weight":104,"slug":105},"Literature",80,"literature",{"id":11,"doc_module":4,"doc_module_name":47,"category_name":12,"show_sort_weight":107,"slug":108},70,"exam",{"id":110,"doc_module":4,"doc_module_name":47,"category_name":111,"show_sort_weight":112,"slug":113},5,"Comic",60,"comic",{"id":115,"doc_module":4,"doc_module_name":47,"category_name":116,"show_sort_weight":117,"slug":118},6,"Technology",50,"technology",{"id":120,"doc_module":4,"doc_module_name":47,"category_name":121,"show_sort_weight":122,"slug":123},7,"Healthcare",40,"healthcare",{"id":22,"doc_module":4,"doc_module_name":47,"category_name":125,"show_sort_weight":126,"slug":127},"Research & Report",30,"research-report",{"id":129,"doc_module":4,"doc_module_name":47,"category_name":130,"show_sort_weight":30,"slug":131},9,"Religion & Spirituality","religion-spirituality",{"id":30,"doc_module":4,"doc_module_name":47,"category_name":133,"show_sort_weight":30,"slug":134},"World Cup","world-cup",{"id":136,"doc_module":4,"doc_module_name":47,"category_name":137,"show_sort_weight":136,"slug":138},10,"Lifestyle","lifestyle",{"id":140,"doc_module":4,"doc_module_name":47,"category_name":141,"show_sort_weight":110,"slug":142},19,"General","general"]