{"id":16248486,"url":"https://github.com/pythonhacker/itertools_idioms","last_synced_at":"2025-07-22T05:03:04.526Z","repository":{"id":72038212,"uuid":"12554413","full_name":"pythonhacker/itertools_idioms","owner":"pythonhacker","description":"Programming Idioms with Python itertools","archived":false,"fork":false,"pushed_at":"2017-05-27T09:34:24.000Z","size":11,"stargazers_count":16,"open_issues_count":0,"forks_count":3,"subscribers_count":7,"default_branch":"master","last_synced_at":"2024-10-11T14:42:08.058Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"bsd-3-clause","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/pythonhacker.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2013-09-03T03:44:31.000Z","updated_at":"2024-03-23T10:54:57.000Z","dependencies_parsed_at":null,"dependency_job_id":"1d319d37-86d7-4e3a-bb00-b1ce6b10e4a4","html_url":"https://github.com/pythonhacker/itertools_idioms","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/pythonhacker/itertools_idioms","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pythonhacker%2Fitertools_idioms","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pythonhacker%2Fitertools_idioms/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pythonhacker%2Fitertools_idioms/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pythonhacker%2Fitertools_idioms/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/pythonhacker","download_url":"https://codeload.github.com/pythonhacker/itertools_idioms/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pythonhacker%2Fitertools_idioms/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":266430671,"owners_count":23927165,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","status":"online","status_checked_at":"2025-07-22T02:00:09.085Z","response_time":66,"last_error":null,"robots_txt_status":null,"robots_txt_updated_at":null,"robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-10-10T14:42:01.531Z","updated_at":"2025-07-22T05:03:04.483Z","avatar_url":"https://github.com/pythonhacker.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"Itertools Idioms\n================\n\nUseful programming idioms with Python itertools.\n\nUsage:\n```python\n\u003e\u003e\u003e from itertools_idioms import idioms\n# Select keys using variable constraints\n\n\u003e\u003e\u003e import operator\n\u003e\u003e\u003e prices = {'cake': 50, 'bread': 20, 'pie': 100}\n\u003e\u003e\u003e constraints = {'cake': (operator.lt, 60), 'bread': (operator.le, 20), 'pie': (operator.lt, 80)}\n\u003e\u003e\u003e list(idioms.select(prices,constraints))\n['cake', 'bread']\n\n# second option - supply your own constraing mapping to functions\n\u003e\u003e\u003e constraints = {'cake': ('less', 60), 'bread': ('less', 30), 'pie': ('lessorequal', 80)}\n\u003e\u003e\u003e cmap = {'less': operator.lt, 'lessorequal': operator.le}\n\u003e\u003e\u003e list(idioms.select(prices,constraints, cmap=cmap))  \n['cake', 'bread']\n\n# best option - use default operator module mapping for comparison constraints.\n\u003e\u003e\u003e constraints = {'cake': ('\u003c', 60), 'bread': ('\u003c=', 20), 'pie': ('\u003c', 80)}\n\u003e\u003e\u003e list(idioms.select(prices,constraints, use_operator=True))\n['cake', 'bread']\n\n# Use select2 if the conditions are directly expressed as functions\n\u003e\u003e\u003e constraints = {'cake': lambda x: x\u003c60,\n...                'bread': lambda x: x\u003c=20,\n...                'pie': lambda x: x\u003c80 }\n\u003e\u003e\u003e list(idioms.select2(prices, constraints))\n['cake', 'bread']\n\n# Use 'call' to return iterables from callables which vary\n# in output when called at different times. Use optional\n# 'times' and 'filter' arguments to control the output.\n\n# Return upto 10 random numbers between 1 and 100\n\u003e\u003e\u003e list(idioms.call(random.randrange, 1, 100, times=10))\n[70, 97, 39, 6, 83, 89, 88, 49, 66, 60]\n\n# Return upto 10 random numbers between 1 and 100 which\n# are multiples of 3\n\u003e\u003e\u003e list(idioms.call(random.randrange, 1, 100, times=10, filter=lambda x: x%3==0))\n[60, 9, 63, 12, 21]\n\n# Random streams - infinite and constrained\n\n# Infinite random stream from your iterable\n\u003e\u003e\u003e for i in idioms.random_stream(iterable): print i\n\n# Specific random streaming functions\n# This would keep going forever\n\u003e\u003e\u003e for i in idioms.random_alphabets(): print i\n\n# This is constrained and stops when it encounters W\n\u003e\u003e\u003e list(idioms.random_alphabets(sentinel='W'))\n['i', 'o', 'z', 'y', 'd', 'Z', 'Y', 's', 'S', 'O', 'Q']\n\n# Infinite generator of random digits    \n\u003e\u003e\u003e idioms.random_digits()\n\u003citertools.imap object at 0xa12f76c\u003e\n\n# Would stop when encountering 8\n\u003e\u003e\u003e list(idioms.random_digits(sentinel=8))\n[4, 2, 3, 2, 9, 5, 9, 3, 7, 7, 5, 5, 3, 5]\n\n# Flatten nested iterators upto any level\n\u003e\u003e\u003e list(flatten([1,[2,[3,[4,[5]]]]]))\n[1, 2, 3, 4, 5]\n\u003e\u003e\u003e list(flatten([1,[2,3],[4,5]]))\n[1, 2, 3, 4, 5]\n\u003e\u003e\u003e list(flatten(dict(enumerate(range(5)))))\n[0, 1, 2, 3, 4]\n\u003e\u003e\u003e list(flatten([1,2,'python',{3:4, 4:5}, ['perl']]))    \n[1, 2, 'python', 3, 4, 'perl']    \n\u003e\u003e\u003e \n\u003e\u003e\u003e 'More coming soon!'\n'More coming soon!'\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpythonhacker%2Fitertools_idioms","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fpythonhacker%2Fitertools_idioms","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpythonhacker%2Fitertools_idioms/lists"}