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

Imagine the following scenario:

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?

Here’s the code from my new book “Python One-Liners“:

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<9 for y in companies[x].values())] print(illegal)


Try it yourself in our interactive Python shell:



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 1,811 08-27-2023, 02:04 PM
Last Post: xSicKxBot
  [Tut] How to Retrieve a Single Element from a Python Generator xSicKxBot 0 1,192 09-30-2022, 11:20 AM
Last Post: xSicKxBot
  [Tut] Python One Line Generator xSicKxBot 0 1,309 09-17-2020, 09:29 PM
Last Post: xSicKxBot
  [Tut] How to Create a List of Dictionaries in Python? xSicKxBot 0 1,194 07-09-2020, 08:37 AM
Last Post: xSicKxBot
  [Tut] Python How to Join a List of Dictionaries into a Single One? xSicKxBot 0 1,389 06-10-2020, 03:34 PM
Last Post: xSicKxBot
  [Tut] How to Filter a List of Dictionaries in Python? xSicKxBot 0 1,332 04-15-2020, 07:33 PM
Last Post: xSicKxBot
  [Tut] Python – How to Sort a List of Dictionaries? xSicKxBot 0 1,470 04-02-2020, 01:25 PM
Last Post: xSicKxBot
  [Tut] What are Regular Expressions Used For? 10 Applications xSicKxBot 0 1,535 03-04-2020, 05:36 AM
Last Post: xSicKxBot
  [Tut] The Python Re Plus (+) Symbol in Regular Expressions xSicKxBot 0 1,590 02-01-2020, 04:38 AM
Last Post: xSicKxBot
  [Tut] Python Re * – The Asterisk Quantifier for Regular Expressions xSicKxBot 0 1,583 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