Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save dgusoff/088705d0fedc4a4a318045cdbfb5aa35 to your computer and use it in GitHub Desktop.
Save dgusoff/088705d0fedc4a4a318045cdbfb5aa35 to your computer and use it in GitHub Desktop.
import time
from datetime import date
greetings = []
greetings.append(dict(name = 'Derek', robot = 'wire01', stamp = time.time()))
greetings.append(dict(name = 'Kyle', robot = 'wire02', stamp = time.time()))
# looking to do something like...
# greetings.where(x => x.name == "Derek" && x.robot == "wire01")
found = False
for greeting in greetings:
if greeting['name'] == "Derek" and greeting['robot'] == 'wire01' and greeting['stamp'] > time.time() - 7200:
found = True
break
print(found)
@kylealanhale
Copy link

The filter top level function is what you're looking for, but it's a purely functional construct alongside map, reduce, etc., not specific to just lists. Here's a succinct, readable, pythonic version:

greetings = [
    {'name': 'Derek', 'robot': 'wire01', 'stamp': time.time()},
    {'name': 'Kyle', 'robot': 'wire02', 'stamp': time.time()}]

def greetings_filter(greeting):
    return greeting['name'] == 'Derek' and greeting['robot'] == 'wire01' and greeting['stamp'] > time.time() - 7200

found = list(filter(greetings_filter, greetings))

print(found)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment