rememberthe tools we are provided with should not be abusedalways pick the right tool for the jobwhich is always to write maintainable code set and dictionary comprehensions comprehensions aren' restricted to lists we can use similar syntax with braces to create sets and dictionaries as well let' start with sets one way to create set is to wrap list comprehension in the set(constructorwhich converts it to set but why waste memory on an intermediate list that gets discardedwhen we can create set directlyhere' an example that uses named tuple to model author/title/genre triadsand then retrieves set of all the authors that write in specific genrefrom collections import namedtuple book namedtuple("book""author title genre"books book("pratchett""nightwatch""fantasy")book("pratchett""thief of time""fantasy")book("le guin""the dispossessed""scifi")book("le guin"" wizard of earthsea""fantasy")book("turner""the thief""fantasy")book("phillips""preston diamond""western")book("phillips""twice upon time""scifi")fantasy_authors author for in books if genre ='fantasy'the highlighted set comprehension sure is short in comparison to the demo-data setupif we were to use list comprehensionof courseterry pratchett would have been listed twice as it isthe nature of sets removes the duplicatesand we end up withfantasy_authors {'turner''pratchett''le guin'we can introduce colon to create dictionary comprehension this converts sequence into dictionary using keyvalue pairs for exampleit may be useful to quickly look up the author or genre in dictionary if we know the title we can use dictionary comprehension to map titles to book objectsfantasy_titles titleb for in books if genre ='fantasy'