Create an account


Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Tut] How to Use Generator Expressions in Python Dictionaries

#1
How to Use Generator Expressions in Python Dictionaries

<div><p><em>Imagine the following scenario: </em></p>
<p>You work in law enforcement for the US Department of Labor, finding companies that pay below minimum wage so you can initiate further investigations. Like hungry dogs on the back of a meat truck, your Fair Labor Standards Act (FLSA) officers are already waiting for the list of companies that violated the minimum wage law. Can you give it to them?</p>
<p>Here’s the code from my new book “<a href="https://www.amazon.com/gp/product/B07ZY7XMX8" target="_blank" rel="noreferrer noopener">Python One-Liners</a>“:</p>
<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">companies = {'CoolCompany' : {'Alice' : 33, 'Bob' : 28, 'Frank' : 29}, 'CheapCompany' : {'Ann' : 4, 'Lee' : 9, 'Chrisi' : 7}, 'SosoCompany' : {'Esther' : 38, 'Cole' : 8, 'Paris' : 18}} illegal = [x for x in companies if any(y&lt;9 for y in companies[x].values())] print(illegal)
</pre>
<figure class="wp-block-embed-youtube wp-block-embed is-type-rich is-provider-embed-handler wp-embed-aspect-16-9 wp-has-aspect-ratio">
<div class="wp-block-embed__wrapper">
<div class="ast-oembed-container"><iframe title="Python One-Liner Trick 8 - Evaluate a Condition in a Dictionary of Dictionaries" width="1400" height="788" src="https://www.youtube.com/embed/_M4dlUPG4XI?feature=oembed" frameborder="0" allow="accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe></div>
</p></div>
</figure>
<p>Try it yourself in our interactive Python shell:</p>
<p> <iframe height="400px" width="100%" src="https://repl.it/@finxter/anygeneratordictionary?lite=true" scrolling="no" frameborder="no" allowtransparency="true" allowfullscreen="true" sandbox="allow-forms allow-pointer-lock allow-popups allow-same-origin allow-scripts allow-modals"></iframe> </p>
</div>


https://www.sickgaming.net/blog/2020/04/...tionaries/
Reply



Possibly Related Threads…
Thread Author Replies Views Last Post
  [Tut] Boolean Operators in Python (and, or, not): Mastering Logical Expressions xSicKxBot 0 817 08-27-2023, 02:04 PM
Last Post: xSicKxBot
  [Tut] How to Retrieve a Single Element from a Python Generator xSicKxBot 0 582 09-30-2022, 11:20 AM
Last Post: xSicKxBot
  [Tut] Python One Line Generator xSicKxBot 0 711 09-17-2020, 09:29 PM
Last Post: xSicKxBot
  [Tut] How to Create a List of Dictionaries in Python? xSicKxBot 0 696 07-09-2020, 08:37 AM
Last Post: xSicKxBot
  [Tut] Python How to Join a List of Dictionaries into a Single One? xSicKxBot 0 880 06-10-2020, 03:34 PM
Last Post: xSicKxBot
  [Tut] How to Filter a List of Dictionaries in Python? xSicKxBot 0 822 04-15-2020, 07:33 PM
Last Post: xSicKxBot
  [Tut] Python – How to Sort a List of Dictionaries? xSicKxBot 0 947 04-02-2020, 01:25 PM
Last Post: xSicKxBot
  [Tut] What are Regular Expressions Used For? 10 Applications xSicKxBot 0 958 03-04-2020, 05:36 AM
Last Post: xSicKxBot
  [Tut] The Python Re Plus (+) Symbol in Regular Expressions xSicKxBot 0 995 02-01-2020, 04:38 AM
Last Post: xSicKxBot
  [Tut] Python Re * – The Asterisk Quantifier for Regular Expressions xSicKxBot 0 945 01-31-2020, 12:39 PM
Last Post: xSicKxBot

Forum Jump:


Users browsing this thread:
1 Guest(s)

Forum software by © MyBB Theme © iAndrew 2016